Question

I'd like to deserialize XML document to a POCO object. But I'm receiving unexpected result. Actually I need to get on output null value for Country property, but it has value with String.Empty.

<?xml version="1.0"?>
<ZIP xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <ZIP>11111</ZIP>
  <CITY>111</CITY>
  <STATE>UT</STATE>
  <COUNTRY xs:Nil="true" />
</ZIP>

Properties looks like:

[XmlElement(IsNullable = true)]
public string ZIP { get; set; }

[XmlElement(IsNullable = true)]
public string CITY { get; set; }

[XmlElement(IsNullable = true)]
public string STATE { get; set; }

[XmlElement(IsNullable = true)]
public string COUNTRY { get; set; }

I know that I can put some hooks with custom properties or implement IXmlSerializable, but it is just one from hundred of XML documents and POCO classes. Is there any generic solution?

Was it helpful?

Solution

Now try it with the correct namespace; xs should here be an alias to "xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance", and it should be xs:nil, not xs:Nil.

<?xml version="1.0"?>
<ZIP xmlns:xs="http://www.w3.org/2001/XMLSchema-instance">
  <ZIP>11111</ZIP>
  <CITY>111</CITY>
  <STATE>UT</STATE>
  <COUNTRY xs:nil="true" />
</ZIP>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top