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.
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()
:
var element = doc.XPathSelectElement("/path/to/element/I/want");
The queries don't have to be simple paths like what you described, they use the XPath language.
Even though this is a somewhat older post, it should be noted that LINQ-to-XML
can be used as an alternative to System.XML.XPath
to find elements based on a path within an XDocument
Example:
var results = x.Elements("path").Elements("to").Elements("element").Elements("I").Elements("want").FirstOrDefault();
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With