XML序列化和空值 - C# [英] XML Serialization and null value - C#

查看:844
本文介绍了XML序列化和空值 - C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有序列化到XML的对象。如果对象的属性之一为空,则本机序列忽略添加此作为属性。这是正常的行为,如果是的话,是有办法,我可以重写它?

例如:

 公共类测试
{
  [XmlAttribute]
  公共字符串值{设置;得到; }  [XmlAttribute]
  公共字符串键{设置;得到; }
}

当值为空,我得到

 <根和GT;
  <试验键=嗒嗒>
< /根>


解决方案

<一个href=\"http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlelementattribute.isnullable%28VS.85%29.aspx\">的XmlElement(ISNULLABLE = TRUE)

I have an object that I serialize to XML. If one of the properties in the object is null, then native serialization ignores to add this as the attribute. Is this normal behavior, if so, is there a way I can override it?

ex:

public class Test
{
  [XmlAttribute]
  public string value {set; get; }

  [XmlAttribute]
  public string key {set; get; }
}

When value is null, i get

<Root>
  <Test key="blah">
</Root>

解决方案

XmlElement( IsNullable = true )

这篇关于XML序列化和空值 - C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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