Pregunta

I am deserializing a string back to an object using C#. The xml string looks like

"<Authentication xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">
  <Status>Success</Status>
  <Available i:nil=\"true\"/>
</Authentication>"

While I'm managed to handle the Available by making the bool property nullable, I'm just wondering what is the correct way to handle i:nil=\"true\"?

Should I being doing something else other than just making a property Available property nullable?

Note that without making the bool property nullable, it throws me an error i.e. string "" cannot be converted to bool, so making nullable sorted my problem, but I'm just curious as to whether or not I should be doing more than just making this nullable

Thanks.

¿Fue útil?

Solución

Your property should be nullable. The XML is explicitly telling you that the property can be either true, false, or null; the 3 are valid values and your class should support all 3.

You don't need to do anything other than making your property a bool?; the XmlSerializer will properly handle reading/writing the i:nil="true" when the property is null.

Just make sure that your code is designed to account for the null values where applicable so you don't get exceptions.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top