I have to deserialize some xml via a webrequest. My deserializer does not deserizlie the meat of the message – but it does not put out an error. It all works if I take out the namespace references on line 2
xml below (edited to hide secret business stuff)
<?xml version="1.0" encoding="UTF-8"?>
<ns2:thingees xmlns:ns2="http://someurl.com">
<thing thing-code="KE">
<thingelement description="primary" thing-address="address24000" sequence="1"/>
<thingelement description="backup" thing-address="address5000" sequence="2"/>
</thing>
<thing thing-code="PI">
<thingelement description="primary" thing-address="address26000" sequence="1"/>
<thingelement description="backup" thing-address="address27000" sequence="2"/>
</thing>
</ns2:thingees>
my classes are as below ( renaming things to hide secret business stuff)
[Serializable]
[XmlRoot("thingees", Namespace = "http://someurl.com")]
public class thingeeInfo
{
[XmlElementAttribute("thing")]
public oneThing[] Items {get; set;}
}
public partial class oneThing
{
[System.Xml.Serialization.XmlElementAttribute("thing-element")]
public ThingElement[] thingelement {get; set;}
[System.Xml.Serialization.XmlAttributeAttribute("thing-code")]
public string thingcode {get; set;}
}
public partial class ThingElement
{
[System.Xml.Serialization.XmlAttributeAttribute()]
public string description {get; set; }
[System.Xml.Serialization.XmlAttributeAttribute("thing-address")]
public string thingaddress {get; set; }
[System.Xml.Serialization.XmlAttributeAttribute()]
public string sequence {get; set; }
}
It all deserializes nicely if I
– take out the namespace references in the root of the xml.
– take out the Namespace reference in the XMlRoot
It deserializes WITHOUT AN ERROR but does not fill the Items – that is, ‘Items’ is null. No ‘things’ are populated
am I supposed to reference the ns2 in the xml? If so, how?
OK, got my answer with more googling.
It looks like you have to add [XmlType(AnonymousType = true)] to the root
and [XmlElement(Form = XmlSchemaForm.Unqualified)] to each attribute and/or element