Search code examples
.netserializationnullablexml-nil

Can you add additional properties to .NET serialized NULL values?


In .NET, you can serialize a nullable element using XmlElement( IsNullable = true ), which results in this:

<SomeElement xsi:nil="true" />.

However, I need to be able to add xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance after any field that is being nulled out.

Example:

<SomeElement xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>

Is this possible?


Solution

  • I figured it out. Here is the answer:

    [XmlElement(IsNullable = true, Namespace = "http://www.w3.org/2001/XMLSchema-instance")]