改变XmlSerializer的序列化如何空元素 [英] change how XmlSerializer serializes empty elements
问题描述
我使用XmlSerializer。它系列化对象就好了,但客户要求所需的空元素是在这种格式<明星:确认/>
。串行器,而不是序列化空元素为<明星:确认>< /星:确认>
有没有办法改变它序列化客户端要求的方式
I am using the XmlSerializer. It serializes the object just fine but the client requires required empty elements to be in this format <star:Confirm/>
. The serializer instead serializes the empty elements to <star:Confirm></star:Confirm>
is there a way to change it to serialize the way the client requires.
推荐答案
尝试不同的事情,我无意中偶然发现了解决方案之后。我设置了 XmlElementAttribute.IsNullable
来像以前的真正答案建议。
After trying different things I accidentally happened upon the solution. I set the XmlElementAttribute.IsNullable
to true like the previous answer suggested.
[System.Xml.Serialization.XmlElementAttribute(ElementName = "Confirm", IsNullable=true)]
public ConfirmType Confirm
{
get
{
return this.confirmField;
}
set
{
this.confirmField = value;
this.RaisePropertyChanged("Confirm");
}
}
设置在我使用的代码的确认类型时
然后。默认的构造函数,而不是设置确认为null
Then when setting the confirm type in the code I used the default constructor instead of setting Confirm to null.
retval.ConfirmBODDataArea.Confirm = new ConfirmType();
这序列化为<星:确认/>
这篇关于改变XmlSerializer的序列化如何空元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!