ENUM 的 XML 序列化为空
这是由 XSD 生成的枚举,
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://tempuri.org/Utenza.xsd")]
public enum MeterType {
/// <remarks/>
[System.Xml.Serialization.XmlEnumAttribute("a diffalco")]
adiffalco,
/// <remarks/>
[System.Xml.Serialization.XmlEnumAttribute("con diffalco")]
condiffalco,
/// <remarks/>
[System.Xml.Serialization.XmlEnumAttribute("di riserva")]
diriserva,
}
并且...这是我用来序列化
String XmlString = null;
MemoryStream memoryStream = new MemoryStream();
XmlSerializer xs = new XmlSerializer(Tipo);
XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
xs.Serialize(xmlTextWriter, pObject);
memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
XmlString = UTF8ByteArrayToString(memoryStream.ToArray());
XML 输出的代码,不包含表示枚举值的属性 rap。 有人可以帮忙吗?多谢。
This is the Enum generated by the XSD
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://tempuri.org/Utenza.xsd")]
public enum MeterType {
/// <remarks/>
[System.Xml.Serialization.XmlEnumAttribute("a diffalco")]
adiffalco,
/// <remarks/>
[System.Xml.Serialization.XmlEnumAttribute("con diffalco")]
condiffalco,
/// <remarks/>
[System.Xml.Serialization.XmlEnumAttribute("di riserva")]
diriserva,
}
and.. this is the code i use to serialize
String XmlString = null;
MemoryStream memoryStream = new MemoryStream();
XmlSerializer xs = new XmlSerializer(Tipo);
XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
xs.Serialize(xmlTextWriter, pObject);
memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
XmlString = UTF8ByteArrayToString(memoryStream.ToArray());
the XML output does not contain the attribute rappresenting the value of the Enum.
Can someone help? Thanks a lot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的问题帮助我解决了近 10 年后我在枚举方面遇到的问题。你现在可能已经找到答案了。您需要有一个 NULL 枚举值,该值将是枚举中的第一个值 (0)。然后使用:
作为用于“Null”枚举值的属性。当 XmlSerialzer 遇到需要映射到枚举的 XML 元素的值 null 或空字符串时,需要执行此操作。
我希望这个答案可以帮助任何有类似问题的人。
Your question helped me solve a problem that I was having with enums close to 10 years later. You probably figured out the answer by now. You need to have an enum value for NULL which would be the first value in the enum (0). Then use:
as the attribute used for the "Null" enum value. This is needed for when the XmlSerialzer encounters the value null or empty string for an XML element that needs tp map to the enum.
I hope this answer helps anyone with a similar problem.