LINQ to XML extract nested elements

前端 未结 2 1875
说谎
说谎 2021-01-29 13:47

I\'m new to LINQ and XML parsing and rather new to C# programming. For the following XML structure, I\'m trying to extract the nested elements:

           


        
2条回答
  •  遥遥无期
    2021-01-29 14:24

    You can do it this way using linq:

    var result = from p in xmlDoc.Descendants("person")
                 from a in p.Descendants("address")
                 where p.Element("personNumber").Value == "2" 
                 select new 
                     { 
                       City = a.Element("city").Value, 
                       Location = a.Element("location").Value 
                     };
    

提交回复
热议问题