You must Sign In to post a response.
  • Category: XML

    How to convert xml to List or Object?

    Dear Team ,

    I have XML from rest web service . how to convert the List or Object ?

    <Get_Menu_UserResponse xmlns="http://tempuri.org/">
    <Get_Menu_UserResult xmlns:a="http://schemas.datacontract.org/2004/07/HHDSynchronization.DataAccess" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <a:MENU_USER>
    <a:USER_ADMINISTRATOR_YN i:nil="true" />
    <a:USER_ASK_PASSWD_FLAG i:nil="true" />
    <a:USER_AUTOFORWARD_WAIT_DAYS>0</a:USER_AUTOFORWARD_WAIT_DAYS>
    <a:USER_AUTOFORWARD_YN_NUM>0</a:USER_AUTOFORWARD_YN_NUM>
    </a:MENU_USER>
    <a:MENU_USER>
    <a:USER_ADMINISTRATOR_YN i:nil="true" />
    <a:USER_ASK_PASSWD_FLAG i:nil="true" />
    <a:USER_AUTOFORWARD_WAIT_DAYS>0</a:USER_AUTOFORWARD_WAIT_DAYS>
    <a:USER_AUTOFORWARD_YN_NUM>0</a:USER_AUTOFORWARD_YN_NUM>
    </a:MENU_USER>
    <a:MENU_USER>
    <a:USER_ADMINISTRATOR_YN i:nil="true" />
    <a:USER_ASK_PASSWD_FLAG i:nil="true" />
    <a:USER_AUTOFORWARD_WAIT_DAYS>0</a:USER_AUTOFORWARD_WAIT_DAYS>
    <a:USER_AUTOFORWARD_YN_NUM>0</a:USER_AUTOFORWARD_YN_NUM>
    </a:MENU_USER>
    <a:MENU_USER>
    <a:USER_ADMINISTRATOR_YN i:nil="true" />
    <a:USER_ASK_PASSWD_FLAG i:nil="true" />
    <a:USER_AUTOFORWARD_WAIT_DAYS>0</a:USER_AUTOFORWARD_WAIT_DAYS>
    <a:USER_AUTOFORWARD_YN_NUM>0</a:USER_AUTOFORWARD_YN_NUM>
    </a:MENU_USER>
    </a:MENU_USER>
    </Get_Menu_UserResult>
    </Get_Menu_UserResponse>
  • #768926
    Hai HemzChand,
    You can convert the XML to List of object by using the below steps:
    1. Create the class which will fulfill to keep this data.
    2. Create the object collection which will be used to carry the data from the above xml file.

    [XmlTypeAttribute(AnonymousType = true)]
    public class MenuData
    {
    [XmlElement("MENU_USER")]
    public List<Menu> Menus { get; set; }

    public MenuData()
    {
    Menus = new List<Menu>();
    }
    }

    public class Menu
    {
    [XmlElement(ElementName = "USER_ADMINISTRATOR_YN")]
    public string USER_ADMINISTRATOR_YN { get; set; }

    [XmlElement(ElementName = "USER_ASK_PASSWD_FLAG")]
    public string USER_ASK_PASSWD_FLAG { get; set; }

    [XmlElement(ElementName = "USER_AUTOFORWARD_WAIT_DAYS")]
    public string USER_AUTOFORWARD_WAIT_DAYS { get; set; }

    [XmlElement(ElementName = "USER_AUTOFORWARD_YN_NUM")]
    public string USER_AUTOFORWARD_YN_NUM { get; set; }
    }

    3. Now try to read the data by de-serializing it:

    var xml = @"<?xml version=""1.0"" ?>
    <MenuUsers>
    <MENU_USER>
    <USER_ADMINISTRATOR_YN i:nil=""true"" />
    <USER_ASK_PASSWD_FLAG i:nil=""true"" />
    <USER_AUTOFORWARD_WAIT_DAYS>0</USER_AUTOFORWARD_WAIT_DAYS>
    <USER_AUTOFORWARD_YN_NUM>0</USER_AUTOFORWARD_YN_NUM>
    </MENU_USER>
    <MENU_USER>
    <USER_ADMINISTRATOR_YN i:nil=""true"" />
    <USER_ASK_PASSWD_FLAG i:nil=""true"" />
    <USER_AUTOFORWARD_WAIT_DAYS>0</USER_AUTOFORWARD_WAIT_DAYS>
    <USER_AUTOFORWARD_YN_NUM>0</USER_AUTOFORWARD_YN_NUM>
    </MENU_USER>
    <MENU_USER>
    <USER_ADMINISTRATOR_YN i:nil=""true"" />
    <USER_ASK_PASSWD_FLAG i:nil=""true"" />
    <USER_AUTOFORWARD_WAIT_DAYS>0</USER_AUTOFORWARD_WAIT_DAYS>
    <USER_AUTOFORWARD_YN_NUM>0</USER_AUTOFORWARD_YN_NUM>
    </MENU_USER>
    <MENU_USER>
    <USER_ADMINISTRATOR_YN i:nil=""true"" />
    <USER_ASK_PASSWD_FLAG i:nil=""true"" />
    <USER_AUTOFORWARD_WAIT_DAYS>0</USER_AUTOFORWARD_WAIT_DAYS>
    <USER_AUTOFORWARD_YN_NUM>0</USER_AUTOFORWARD_YN_NUM>
    </MENU_USER>
    </MenuUsers>";
    var serializer = new XmlSerializer(typeof(MenuData), new XmlRootAttribute("MenuUsers"));
    using (var stringReader = new StringReader(xml))
    using (var reader = XmlReader.Create(stringReader))
    {
    var result = (MenuData) serializer.Deserialize(reader);
    Console.WriteLine(result.Menus[1].USER_AUTOFORWARD_WAIT_DAYS);
    }

    You can also follow the below link for code snippet:

    http://stackoverflow.com/questions/5854641/convert-xml-to-list-by-deserialize-in-c-sharp

    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com

  • #768959
    Here is the example of code snippet to convert xml to List or Object?
     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);
    }
    }


    Reference: http://stackoverflow.com/questions/5854641/convert-xml-to-list-by-deserialize-in-c-sharp


Sign In to post your comments