Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert xml to List by Deserialize in c#

Tags:

c#

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......

like image 349
Mou Avatar asked May 02 '11 07:05

Mou


Video Answer


1 Answers

This should work:

[XmlElement("CustomerRet")]
public List<Customer> Customers { get; set; }

And a full example:

[XmlTypeAttribute(AnonymousType = true)]
public class CustomersData
{
    [XmlElement("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; }
}

class Program
{
    static void Main()
    {
        var xml = 
@"<?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>";
        var serializer = new XmlSerializer(typeof(CustomersData), new XmlRootAttribute("CustomerQueryRs"));
        using (var stringReader = new StringReader(xml))
        using (var reader = XmlReader.Create(stringReader))
        {
            var result = (CustomersData)serializer.Deserialize(reader);
            Console.WriteLine(result.Customers[1].FullName);
        }
    }
}
like image 195
Darin Dimitrov Avatar answered Nov 13 '22 09:11

Darin Dimitrov