Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Most elegant way to query XML string using XPath

Tags:

c#

xml

xpath

I'm wondering what the most elegant way is in C# to query a STRING that is valid xml using XPath?

Currently, I am doing this (using LINQ):

var el = XElement.Parse(xmlString);
var h2 = el.XPathSelectElement("//h2");
like image 421
Scott Klarenbach Avatar asked Aug 17 '09 18:08

Scott Klarenbach


People also ask

What is XPath expression in XML?

XPath uses path expressions to select nodes or node-sets in an XML document. These path expressions look very much like the expressions you see when you work with a traditional computer file system. XPath expressions can be used in JavaScript, Java, XML Schema, PHP, Python, C and C++, and lots of other languages.

What is an XPath query?

XPath (XML Path Language) is a query language that can be used to query data from XML documents. In RUEI, XPath queries can be used for content scanning of XML documents. A complete specification of XPath is available at http://www.w3.org/TR/xpath .

How can you identify multiple elements with the same name in XPath?

If an xpath refers multiple elements on the DOM, It should be surrounded by brackets first () and then use numbering. if the xpath refers 4 elements in DOM and you need 3rd element then working xpath is (common xpath)[3].

What is XPath in XML?

XPath uses path expressions to select nodes or node-sets in an XML document. These path expressions look very much like the expressions you see when you work with a traditional computer file system. XPath expressions can be used in JavaScript, Java, XML Schema, PHP, Python, C and C++, and lots of other languages.

How to find the XPath of a web element using nested attributes?

Within all the tables find the column which has text ‘Author’. Target web element’s XPath can also be generated using the nested attributes. For Example, in this case, it’ll look for a specific attribute across DOM and then look for another attribute within it.

How do I query an XML file in PowerShell?

PowerShell is no different. PowerShell has a few different ways to query XML files, but this tip will focus on the Select-Xml cmdlet and XPath syntax. XPath is a syntax for defining parts of an XML document. XPath has been around since 1999 and has been used as a standard way to query XML documents. XPath defines an XML document as a tree.

How many ways to write ultimate XPath for any type of Web element?

20 ways to write ultimate XPATH for ANY type of web element (XPATH will never be invalid) A web application is made up of different types of web elements such as web element for a button to click on, input web element to type text, dropdown, radio buttons etc.


2 Answers

Simple example using Linq to XML :

XDocument doc = XDocument.Parse(someStringContainingXml);
var cats = from node in doc.Descendants("Animal")
           where node.Attribute("Species").Value == "Cat"
           select node.Attribute("Name").Value;

Much clearer than XPath IMHO...

like image 51
Thomas Levesque Avatar answered Oct 26 '22 03:10

Thomas Levesque


Just for the record, I did not want to go with Linq2XML but XPath and found this way:

var xPathDoc = new XPathDocument(new StringReader("your XML string goes here"));
like image 45
Marc Wittke Avatar answered Oct 26 '22 03:10

Marc Wittke