How to get elements by name in XML using LINQ

后端 未结 2 1122
暗喜
暗喜 2021-01-04 05:41

I\'ve chosen the title here as my problem is I need to get the Item nodes mentioned in the example. I have the following XML and am having problems using LINQ to query it, I

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-04 06:30

    I had a blank Namespace Declaration in my XML I hadn't noticed once I added this into my code it worked - forgot LINQ is very NameSpace oriented!

    XNamespace ns = "http://example.org/namespace";
    var ids = element.Element(ns + "items") 
                     .Elements("item") 
                     .Select(item => item.Element("id").Value); 
    

提交回复
热议问题