I have following xml which I want to Deserialize to an object.
<result>
<reporttype>2</reporttype>
<items>
<item>
<sku>0B0005</sku>
<style>0B0005.DAK.GREY</style>
<reason>Barcode cannot be moved to different SKUs</reason>
</item>
<item>
<sku>0B0006</sku>
<style>0B0006.DAK.GREY</style>
<reason>Barcode cannot be moved to different SKUs</reason>
</item>
</items>
</result>
But following code does not populate the item list, Can someone point me out what I am doing wrong here
string inputString = @"<result><reporttype>2</reporttype><items><item><sku>0B0005</sku><style>0B0005.DAK.GREY</style><reason>Barcode cannot be moved to different SKUs</reason></item><item><sku>0B0005</sku><style>0B0005.DAK.GREY</style><reason>Barcode cannot be moved to different SKUs</reason></item></items></result>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(inputString);
XmlSerializer serializer = new XmlSerializer(typeof(Result));
StringReader rdr = new StringReader(doc.InnerXml);
Result resultingMessage = (Result)serializer.Deserialize(rdr);
public enum ReportType {
[XmlEnum("0")]
InternalErrorReport,
[XmlEnum("1")]
ErrorReport,
[XmlEnum("2")]
InternalSuccessReport
}
[XmlRoot(ElementName = "result")]
public class Result {
[XmlElement(ElementName = "reporttype")]
public ReportType reportType { get; set; }
[XmlElement(ElementName = "items")]
public List<Item> items = new List<Item>();
public string error { get; set; }
public class Item {
[XmlElement(ElementName = "sku")]
string sku { get; set; }
[XmlElement(ElementName = "style")]
string style { get; set; }
[XmlElement(ElementName = "reason")]
string reason { get; set; }
}
}
Or is there a better way to do this?
XmlElementattribute to theItemclass, so the serializer doesn't know that<item>element is anItemclass instance.[XmlArray("items")] and [XmlArrayItem("items")].