i have a xml file and i am trying to populate my list with the data through deserialization.
my xml is here
<?xml version="1.0" ?>
<CustomerQueryRs>
<CustomerRet>
<ListID>6BE0000-1159990808</ListID>
<Name>+ Blaine Bailey</Name>
<FullName>+ Blaine Bailey</FullName>
<Phone>866-855-0800</Phone>
</CustomerRet>
<CustomerRet>
<ListID>9BA0000-1165353294</ListID>
<Name>+ Brian Boyd</Name>
<FullName>+ Brian Boyd</FullName>
<Phone>203-245-1877</Phone>
</CustomerRet>
<CustomerRet>
<ListID>9280000-1164147562</ListID>
<Name>+ Brian Leahy</Name>
<FullName>+ Brian Leahy</FullName>
<Phone>508-341-0955</Phone>
</CustomerRet>
</CustomerQueryRs>
here i am giving my full code. i just do not understand why my code is not working…what is missing in my code……it is not giving error but list is not getting populated. so please tell me which area i need to rectify in code.
[XmlTypeAttribute(AnonymousType = true)]
public class CustomersData
{
[XmlArray(ElementName = "CustomerQueryRs")]
[XmlArrayItem(ElementName = "CustomerRet")]
public List<Customer> Customers { get; set; }
public CustomersData()
{
Customers = new List<Customer>();
}
}
public class Customer
{
[XmlElement(ElementName = "ListID")]
public string ListID { get; set; }
[XmlElement(ElementName = "Name")]
public string Name { get; set; }
[XmlElement(ElementName = "FullName")]
public string FullName { get; set; }
[XmlElement(ElementName = "Phone")]
public string Phone { get; set; }
}
here is my desirialization code
private object DeserialzeXml(string xml)
{
var xmlSer = new XmlSerializer(typeof(CustomersData), new XmlRootAttribute("CustomerQueryRs"));
var stringReader = new StringReader(xml);
return xmlSer.Deserialize(stringReader);
}
please help……
This should work:
And a full example: