LINQ query to parse content from XML to class

前端 未结 3 1184
广开言路
广开言路 2021-01-28 13:35

I have an xml from which I am trying to extract some information through LINQ query.
The format of the xml file looks like the following:


           


        
3条回答
  •  青春惊慌失措
    2021-01-28 14:25

    hi i think this is what you expect.

    List lstXElements = new List();
            lstXElements.AddRange(GetDescendants("NextItem"));
            lstXElements.AddRange(GetDescendants("PreviousItem"));
    
            List lstExtract = new List();
    
            foreach (XElement objElement in lstXElements)
            {
                Extract objExtract = new Extract();
                objExtract._id = Convert.ToInt32(objElement.Attribute("id").Value);
                objExtract.name = (objElement.Name).LocalName;
                lstExtract.Add(objExtract);
            }
    List GetDescendants(string strDescentName)
        {
            return ((XDocument.Load(Server.MapPath("XMLFile1.xml"))
                .Descendants(strDescentName))
                ).ToList();
        }
    

提交回复
热议问题