Search code examples
c#xmlnullxml-deserializationxsi

XML xsi:nil="true" value set but returning null


I have given XML doc:

<root>
   <element xsi:nil="true" />
<root>

Later on in code the element gets updated to:

<root>
  <element xsi:nil="true">some data</element>
</root>

No whey I try to deserialize the element in to an object the "element" property is null. What am I doing wrong?

Here is the C# code I use to deserialize:

XmlSerializer reader = new XmlSerializer(typeof(MyObject));
StreamReader srFile = new StreamReader(strFilePath);
MyObject obj = (MyObject)reader.Deserialize(srFile);

[Serializable]
public class My Object
{
  [XmlElement(IsNullable = true)]
  public string Element {get; set;}
}

Solution

  • I was able to do a nodeName.RemoveAttribute("xsi:nil") before setting the value. Seems like there should be a way to do this when setting the element value since the value of the element would no longer be null.