XSD.EXE生成的类不会序列化的默认值属性 [英] xsd.exe generated classes don't serialize default value attributes
问题描述
我有一个XSD架构由XSD.EXE产生一些的.cs文件。
I have few .cs files generated by xsd.exe by a XSD schema.
我的问题是,当我尝试序列这些类XML,其值匹配XSD架构中定义的默认值没有被序列化的属性。
My problem is that when i try to serialize those classes to xml, the attributes whose values match the default values defined in the xsd schema are not being serialized.
我发现,当我删除 [System.ComponentModel.DefaultValueAttribute(typeof运算(小于我的问题就解决了; SOMETYPE>),< someValue中方式>),用以表示默认字段成员]
属性。
I found out that my problem is solved when i remove [System.ComponentModel.DefaultValueAttribute(typeof(<someType>), "<SomeValue>")]
attribute for the member representing a default field.
所以我的问题是不是有更优雅的方式去做这个? ?例如有没有什么办法可以指定我想要的默认值进行反正序列
So my question isn't there more elegant way to do this? For example is there any way to specify that I want the default values to be serialized anyway?
推荐答案
短的版本是:是。这看起来是最简单的方法。
The short versions is: yes. it looks like that is the simplest approach.
我试过了显而易见的东西(尤其是 *指定
和 ShouldSerialize *
模式,的XmlSerializer
支持,还 ISNULLABLE
),它只是不断不包括价值。
I've tried the "obvious" things (in particular the *Specified
and ShouldSerialize*
patterns that XmlSerializer
supports, but also IsNullable
) and it just keeps on not including the value.
接下来的选择是,要么的IXmlSerializable
(其中是平原硬),或 XmlAttributeOverrides
(重复劳动) - 两者都不是特别诱人这里。我不知道你目前的办法是减少工作?你当然可以简单地导出WSDL和编辑删除默认的(和编辑后的版本生成)
The next options would be either IXmlSerializable
(which is plain hard), or XmlAttributeOverrides
(a duplication of effort) - neither of which is particularly enticing here. I wonder if your current approach is less work? You could of course simply export the WSDL and edit that to remove the default (and generate from the edited version).
顺便说一句 - 如果这是WCF,有在 [数据成员(EmitDefaultValue = ...)]
的确实的控制这一点;也许他们加入这个作为一个功能提出要求?
As an aside - if this was WCF, there is the [DataMember(EmitDefaultValue = ...)]
that does control this; maybe they added this as a feature request?
这篇关于XSD.EXE生成的类不会序列化的默认值属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!