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?

有帮助吗?

解决方案

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>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top