可选属性默认值的 XML 序列化
我有一组使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是因为您将默认值指定为“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.
在序列化之前,您必须将 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.