How do I find a XML node by path in Linq-to-XML

前端 未结 2 1915
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-12 10:37

If I get the path to a specific node as a string can I somehow easily find said node by using Linq/Method of the XElement ( or XDocument ).

There are so many differe

2条回答
  •  生来不讨喜
    2021-01-12 11:24

    You can also use XPathEvaluate

    XDocument document = XDocument.Load("temp.xml");
    var found = document.XPathEvaluate("/documents/items/item") as IEnumerable;
    foreach (var obj in found)
    {
        Console.Out.WriteLine(obj);    
    }
    
    
    

    Given the following xml:

    
    
      
        
        
      
    
    

    This should print the contents from the items node.

    提交回复
    热议问题