문제

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