ENUM 的 XML 序列化为空

发布于 2024-11-18 03:18:39 字数 1032 浏览 0 评论 0原文

这是由 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妞丶爷亲个 2024-11-25 03:18:39

你的问题帮助我解决了近 10 年后我在枚举方面遇到的问题。你现在可能已经找到答案了。您需要有一个 NULL 枚举值,该值将是枚举中的第一个值 (0)。然后使用:

[System.Xml.Serialization.XmlEnumAttribute("")] 

作为用于“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:

[System.Xml.Serialization.XmlEnumAttribute("")] 

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文