我在序列化枚举值时遇到问题。
这是代码:
[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");
结果是:
<?xml version="1.0" encoding="utf-8" ?>
<REQUEST ID="1234" />
为什么枚举没有序列化?我使用.NET Framework 2.0。
谢谢。
我发现出了什么问题。对于每个枚举类型
[System.Xml.Serialization.XmlAttributeAttribute()]
public REQUESTTypetype Type;
我懂了:
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool TypeSpecified;
在代码中我应该这样做:
request.Type = REQUESTTypetype.One;
request.TypeSpecified = true;
现在效果很好。我应该将它们发布在我的问题中,但我根本没有注意这些“指定”成员。
感谢您的回复。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)