I would like to query an XDocument
object for a given path, (e.g. "/path/to/element/I/want") but I don't know how to proceed.
相关问题
- Sorting 3 numbers without branching [closed]
- Illegal to have multiple roots (start tag in epilo
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
I needed to do something similar. This repo has several unit tests demoing XDocument querying with XPathEvaluate()
Even though this is a somewhat older post, it should be noted that
LINQ-to-XML
can be used as an alternative toSystem.XML.XPath
to find elements based on a path within anXDocument
Example:
Note: The LINQ to XML command may need to be altered to accommodate for the actual structure and/or cardinality of the XML.
https://msdn.microsoft.com/en-us/library/bb675156.aspx
Something similar to this might work:
You can use methods from
System.Xml.XPath.Extensions
to do this.For example, if you want to select a single element, you would use
XPathSelectElement()
:The queries don't have to be simple paths like what you described, they use the XPath language.