tags:

views:

1247

answers:

3

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>
+2  A: 

XmlElement( IsNullable = true )

Sunny
A: 

In case Sunny's answer just doesn't fits to you, you can customize the serialization process by implementing the IXmlSerializable interface

Jhonny D. Cano -Leftware-
A: 

For some background, take a look at the following ibm article Representations of null in XML Schema

Additionally check out the answer SO question Serialize a nullable int may be helpful in your efforts.

Robert Paulson