XmlSerializer的。保持空字符串属性? [英] XmlSerializer. Keep null string properties?

查看:988
本文介绍了XmlSerializer的。保持空字符串属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  

可能重复:
   XML序列化和空值 - C#
  <一href="http://stackoverflow.com/questions/3935582/change-how-xmlserializer-serializes-empty-elements">change XmlSerializer的是如何序列化空元素

如何使XmlSerializer的存储空标签具有字符串属性为空值,而不是跳过这个属性?

解决方案

你的意思是你想这样的:

 &LT;父&GT;
    &LT; child1&GT;的Hello World&LT; / child1&GT;
    &LT;的child2 /&GT;
&LT; /父&GT;
 

而不是

 &LT;父&GT;
    &LT; child1&GT;的Hello World&LT; / child1&GT;
&LT; /父&GT;
 

你的类应该是这样的: 串行器调用 ShouldSerializePropertyName 方法,通过定义(如果存在的话),以确定是否一个属性应序列(如Windows窗体设计器,太)。

 公共类父
{
    [的XmlElement(Child1)]
    公共字符串Child1 {获得;组; }

    [的XmlElement(CHILD2)]
    公共字符串CHILD2 {获得;组; }

    公共BOOL ShouldSerializeChild2(){返回true; }

}
 

Possible Duplicate:
XML Serialization and null value - C#
change how XmlSerializer serializes empty elements

How to make XmlSerializer store empty tags for string properties having null values, instead of skipping this property?

解决方案

You mean you want this:

<parent>
    <child1>Hello World</child1>
    <child2 />
</parent>

instead of

<parent>
    <child1>Hello World</child1>
</parent>

your class should look like this: The serializer calls a ShouldSerializePropertyName method by definition (if exists) to determine if a property should be serialized (like Windows Forms Designer, too).

public class Parent
{
    [XmlElement("Child1")]
    public string Child1 { get; set; }

    [XmlElement("Child2")]
    public string Child2 { get; set; }

    public bool ShouldSerializeChild2() { return true; }

}

这篇关于XmlSerializer的。保持空字符串属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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