views:

333

answers:

2

I've created an object with 100+ elements and not all of them are showing up in the final XML after serialization. What can I add to the [XmlElement] decorator to make sure it is in the final XML even if it is empty?

A: 

You can use:-

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

However this also includes the xsi namespace and adds xsi:nil = "true" attribute to the element.

AnthonyWJones
+1  A: 

use the "IsNullable" property

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

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

free-dom