Worked with Json until now. Now I get an XML as data source and I would like to Deserialize it into C# objects using the XML attributes. The problem, as I see it is that I would like to serialize the XML only from MSGData - so I set the DOCUMENT as XmlRootAttribute. Currently there is no error - just getting the properties as null.
This is my XML (I need only the C# minimum needed classes with the right attributes) :
<?xml version="1.0" encoding="IBM862"?>
<DOCUMENT>
<MSGData>
<ITEMS>
<ITEM>
<ID>121</ID>
<Name>test</Name>
</ITEM>
<ITEM>
<ID>122</ID>
<Name>test1</Name>
</ITEM>
<ITEM>
<ID>122</ID>
<Name>test1</Name>
</ITEM>
</ITEMS>
</MSGData>
</DOCUMENT>
This is how my classes looks like (ignore typos errors - this is not copy+paste code - it is free text):
<Xmlroot(ElementName="ITEM")]
public class Item
{
[XmlElement(ElementName="ID"])
public int ID {get; set;}
[XmlElement(ElementName="Name"])
public string Name {get; set;}
}
[Xmlroot(ElementName="ITEMS")]
public class Items
{
[XmlElement(ElementName="ITEM"]) //I think that here I should use XmlArray (see below)
//[XmlArray("ITEMS"]
IEnumerable<Item> Items {get; set;}
}
[XmlRoot(ElementName="MSGData")]
public class MSGData
{
[XmlElement(ElementName="ITEMS")]
public Items itemsInstance {get; set;}
}
when I read the XML I use the XmlRootAttribute to start gather the data inside the DOCUMENT node:
XmlRootAttribute xRoot = new XmlRootAttribute("DOCUMENT");
XmlSerializer xmls = new XmlSerializer(typeof(MSGData), xRoot);
MSGData messageData = (MSGData)xmls.Deserialize(new
StringReader(response.Content.ReadAsStringAsync().Result));