我想知道用"LINQ"实现这个xpath查询的"最佳实践"方式(在C#中)是什么:
/topNode/middleNode[@filteringAttribute='filterValue']/penultimateNode[@anotherFilterAttribute='somethingElse']/nodesIWantReturned
Run Code Online (Sandbox Code Playgroud)
我想要'nodesIWantReturned'的IEnumerable列表,但只能从XML树的某个部分开始,这取决于祖先属性的值.
Gre*_*ech 10
除了显示的Linq方法之外,您还可以导入System.Xml.XPath命名空间,然后使用XPathSelectElements扩展方法直接使用XPath查询.
在课堂上注意到这些方法比"正确的"Linq-to-XML要慢,但是很多时候这个方法并不太重要,有时候使用XPath会更容易(当然它很简单!) .
var result = doc.XPathSelectElements("your xpath here");
Run Code Online (Sandbox Code Playgroud)