How to get elements by name in XML using LINQ

后端 未结 2 1121
暗喜
暗喜 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); 
    
    0 讨论(0)
  • 2021-01-04 06:35

    Assuming element is your <a:entry> element:

    var ids = element.Element("items")
                     .Elements("item")
                     .Select(item => item.Element("id").Value);
    

    The Element and Elements methods return only direct children, not all descendants, so it doesn't return the <items> element which is under <data>

    0 讨论(0)
提交回复
热议问题