我正在使用 XmlSerializer。它可以很好地序列化对象,但客户端需要采用这种格式的空元素<star:Confirm/>
。相反,序列化程序将空元素序列化为<star:Confirm></star:Confirm>
是否有办法将其更改为序列化客户端所需的方式。
问问题
15931 次
2 回答
7
在尝试了不同的事情之后,我意外地遇到了解决方案。我将 设置XmlElementAttribute.IsNullable
为 true 就像之前建议的答案一样。
[System.Xml.Serialization.XmlElementAttribute(ElementName = "Confirm", IsNullable=true)]
public ConfirmType Confirm
{
get
{
return this.confirmField;
}
set
{
this.confirmField = value;
this.RaisePropertyChanged("Confirm");
}
}
然后在代码中设置确认类型时,我使用默认构造函数而不是将 Confirm 设置为 null。
retval.ConfirmBODDataArea.Confirm = new ConfirmType();
这序列化为<star:Confirm/>
于 2010-10-14T20:35:54.990 回答
3
您可以尝试将XmlElementAttribute.IsNullable
属性设置为true
. 但是,请记住xsi:nil="true"
属性将作为结果输出。
于 2010-10-14T17:16:34.187 回答