可选属性默认值的 XML 序列化

发布于 2024-09-13 10:20:50 字数 1443 浏览 2 评论 0原文

我有一组使用 xsd.exe 构建的类,并且我正在尝试序列化它们。但是,生成的 XML 中不包含属性。这是问题所在的架构的一部分。

<xsd:element name="Widget">
    <xsd:complexType>
        /* sequence removed for brevity */
        <xsd:attribute name="Version" type="Version" use="optional" default="1.1"/>
    </xsd:complexType>
</xsd:element>
<xsd:simpleType name="Version">
    <xsd:restriction base="xsd:string">
        <xsd:enumeration value="1.0"/>
        <xsd:enumeration value="1.1"/>
    </xsd:restriction>
</xsd:simpleType>

xsd.exe 在“Widget”类上生成了一个名为“Version”的属性和另一个名为“VersionSpecified”的属性,但是当我序列化时,即使设置为 true,这似乎也不会生成属性:

[XmlAttributeAttribute]
[DefaultValueAttribute(Version.Version_1_1)]
public Version Version { get; set; }

[Serialization.XmlIgnoreAttribute]
public bool VersionSpecified { get; set; }

这是它所在的枚举based:

/// <remarks/>
[GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[Serializable]
public enum Version
{
    [XmlEnumAttribute("1.0")]
    Version_1_0,

    [XmlEnumAttribute("1.1")]
    Version_1_1,
}

根据请求的代码片段

Widget widget = new Widget();
widget.Version = Version.Version_1_1;
widget.VersionSpecified = true;    

XmlSerializer serializer = new XmlSerializer(widget.GetType());
serializer.Serialize(/*Memory Stream object*/, widget);

有人对为什么序列化拒绝引入该属性有任何想法吗?

I have a set of classes build using xsd.exe, and I am trying to serialise them. However, an attribute is not being included in the resulting XML. Here is part of the schema where the problem lies.

<xsd:element name="Widget">
    <xsd:complexType>
        /* sequence removed for brevity */
        <xsd:attribute name="Version" type="Version" use="optional" default="1.1"/>
    </xsd:complexType>
</xsd:element>
<xsd:simpleType name="Version">
    <xsd:restriction base="xsd:string">
        <xsd:enumeration value="1.0"/>
        <xsd:enumeration value="1.1"/>
    </xsd:restriction>
</xsd:simpleType>

xsd.exe generated a property called "Version" on a "Widget" class and another property called "VersionSpecified", but this not appear to generate the attribute when I serialize even when set to true:

[XmlAttributeAttribute]
[DefaultValueAttribute(Version.Version_1_1)]
public Version Version { get; set; }

[Serialization.XmlIgnoreAttribute]
public bool VersionSpecified { get; set; }

And this is the enumeration on which it is based:

/// <remarks/>
[GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[Serializable]
public enum Version
{
    [XmlEnumAttribute("1.0")]
    Version_1_0,

    [XmlEnumAttribute("1.1")]
    Version_1_1,
}

Code snippet as per request

Widget widget = new Widget();
widget.Version = Version.Version_1_1;
widget.VersionSpecified = true;    

XmlSerializer serializer = new XmlSerializer(widget.GetType());
serializer.Serialize(/*Memory Stream object*/, widget);

Does anyone have any thoughts on why the serialization refuses to introduce the attribute?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

三生池水覆流年 2024-09-20 10:20:50

这是因为您将默认值指定为“1.1”。当属性等于其默认值时,序列化程序不会创建元素/属性。

Its because you specified the default value as "1.1". The serialiser will not create the element/attribute when the property is equal to its default value.

热血少△年 2024-09-20 10:20:50

在序列化之前,您必须将 VersionSpecified 标志设置为 true。这就是它如何知道是否要序列化此可选属性。

You have to set the VersionSpecified flag to true before serializing. That's how it knows whether or not this optional attribute is to be serialized.

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