枚举的XML序列化 [英] XML serialization of enums
问题描述
这是代码:
我有序列化枚举值的问题。 code> [System.Xml.Serialization.XmlRootAttribute(Namespace =,IsNullable = false)]
public class REQUEST
{
[System.Xml.Serialization.XmlAttributeAttribute() ]
public string ID;
[System.Xml.Serialization.XmlAttributeAttribute()]
public REQUESTTypetype类型;
}
公开枚举REQUESTTypetype
{
一,
二,
三,
四,
}
...
REQUEST request = new REQUEST();
request.ID =1234;
request.Type = REQUESTTypetype.One;
XmlDocument doc = new XmlDocument();
MemoryStream ms = new MemoryStream();
StreamWriter sw = new StreamWriter(ms);
XmlSerializer xs = new XmlSerializer(typeof(REQUEST));
xs.Serialize(sw,request_group);
ms.Position = 0;
doc.Load(ms);
TestWriteXml(doc,@C:\xml_test.xml);
结果是:
<?xml version =1.0encoding =utf-8?>
< REQUEST ID =1234/>
为什么枚举不是序列化的?我使用.NET Framework 2.0。
谢谢。
我发现有什么问题对于每个枚举类型
[System.Xml.Serialization.XmlAttributeAttribute()]
public REQUESTTypetype类型;
我得到这个:
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool TypeSpecified;
在代码中我应该这样做:
request.Type = REQUESTTypetype.One;
request.TypeSpecified = true;
现在可以正常工作。我应该在我的问题上发表他们,但我没有注意这些指定的成员。
感谢您的回复。
I have problems with serializing enum values.
Here is the code:
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public class REQUEST
{
[System.Xml.Serialization.XmlAttributeAttribute()]
public string ID;
[System.Xml.Serialization.XmlAttributeAttribute()]
public REQUESTTypetype Type;
}
public enum REQUESTTypetype
{
One,
Two,
Three,
Four,
}
...
REQUEST request = new REQUEST();
request.ID = "1234";
request.Type = REQUESTTypetype.One;
XmlDocument doc = new XmlDocument();
MemoryStream ms = new MemoryStream();
StreamWriter sw = new StreamWriter(ms);
XmlSerializer xs = new XmlSerializer(typeof(REQUEST));
xs.Serialize(sw, request_group);
ms.Position = 0;
doc.Load(ms);
TestWriteXml(doc, @"C:\xml_test.xml");
The result is:
<?xml version="1.0" encoding="utf-8" ?>
<REQUEST ID="1234" />
Why the enum is not serialized? I use .NET Framework 2.0.
Thank you.
I found what was wrong. For every enum type
[System.Xml.Serialization.XmlAttributeAttribute()]
public REQUESTTypetype Type;
I got this:
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool TypeSpecified;
And in the code I should do this:
request.Type = REQUESTTypetype.One;
request.TypeSpecified = true;
It works fine now. I should have post them in my question but I did not pay attention to these "specified" members at all. Thanks for your replies.
这篇关于枚举的XML序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!