可选属性的默认值XML序列化 [英] XML Serialization of the default values of optional attributes

查看:187
本文介绍了可选属性的默认值XML序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组类生成使用XSD.EXE,和我想连载他们。然而,属性不被包括在生成的XML。下面是模式的一部分问题出在哪里。

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生成一个名为版本的属性上的窗口小部件级和所谓的VersionSpecified另一个属性,但是这似乎没有产生该属性设置为true时,当我连序列:

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段为每个请求

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?

推荐答案

它,因为你指定的默认​​值为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.

这篇关于可选属性的默认值XML序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆