locked
Problem in getting the node values of same node names from an xml to a list using linq RRS feed

  • Question

  • User483994611 posted

    Basically i want to get the node names from an xml to a list<> by linq It may be

    var list=new List<string>

    or in the form of List<User>

    i have the xml file like

    <?xml  version="1.0" encoding="utf-8"?>
    <RolePrev xmlns="http://schemas.microsoft.com">
      <RoleUser>
        <Role>Admin</Role>
        <Previleges>Add</Previleges>
        <Previleges>Edit</Previleges>
        <Previleges>Del</Previleges>
        <Previleges>View</Previleges>
      </RoleUser>
    </RolePrev>

    My Class is like

    [Serializable]
        [XmlRoot("RolePrev")]
        public class User
        {


            public User()
            {

            }
            public int ID { get; set; }
            public string Name { get; set; }
            [XmlArray(ElementName = "Previleges")]
            public List<string> Previleges { get; set; }
            [XmlElement(ElementName = "Role")]
            public string Role { get; set; }
        }

    i want to deserialize it to List<User>

    string filepath = Server.MapPath("XML/RolePrev.xml");  

    User objUser = new User();
                XmlDocument xdoc = new XmlDocument();
                xdoc.Load(filepath);
                objUser.Role = xdoc.GetElementsByTagName("Role")[0].InnerText;
                objUser.Previleges = new List<string>();
                XmlNodeList list = xdoc.GetElementsByTagName("Previleges");
                for (int i = 0; i < list.Count; i++)
                {

                    XmlElement Previleges = (XmlElement)xdoc.GetElementsByTagName("Previleges")[i];
                    
                    objUser.Previleges.Add(Previleges.InnerText);

                }

    I want to use linq instead .please help me..like:-

     string filepath = Server.MapPath("XML/RolePrev.xml");   
                XDocument xdocument = XDocument.Load(filepath);
                var query = xdocument.Descendants("Previleges").ToList();

    query will return list of previleges....plz help

              

    Monday, January 7, 2013 2:13 PM

All replies

  • User1648350404 posted

    You can easily deserialize with XmlSerializer, have a look at following:

    http://stackoverflow.com/questions/364253/how-to-deserialize-xml-document

    Tuesday, January 8, 2013 6:28 AM
  • User483994611 posted

    I have found a way using linq

    XDocument xdocument = XDocument.Load(filepath);
               
                XNamespace nm = XNamespace.Get("http://schemas.microsoft.com");
                List<string> prev = xdocument.Descendants(nm + "Previleges")
                                       .Select(x => x.Value)
                                       .ToList();
                objUser.Previleges = new List<string>();
                foreach (string str in prev)
                {
                    objUser.Previleges.Add(str);
                }

    But is this the best way..using linq??

    Tuesday, January 8, 2013 2:03 PM