即使缺少值,Xml序列化对象和添加元素

时间:2009-08-25 20:45:36

标签: c# .net xml-serialization

我创建了一个包含100多个元素的对象,并不是所有这些元素都在序列化后出现在最终的XML中。我可以添加什么来添加到[XmlElement]装饰器中以确保它在最终的XML中,即使它是空的?

2 个答案:

答案 0 :(得分:2)

使用“IsNullable”属性

public class Person
{
    [XmlElement(IsNullable = true)]
    public string Name { get; set; }
}

http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlelementattribute.isnullable.aspx

答案 1 :(得分:0)

您可以使用: -

[XmlElement(IsNullable = true)]
public string MustBePresent;

但是,这也包括xsi名称空间,并为元素添加xsi:nil = "true"属性。