Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 different types of XML objects it would also be nice if as a added bonus you could point me to a guide on why/how to use different types.

EDIT: Ok after being pointed towards XPathSelectElement I'm trying it out so I can give him the right answer I can't quite get it to work though. This is the XML I'm trying out

<Product>
  <Name>SomeName</Name>
  <Type>SomeType</Type>
  <Quantity>Alot</Quantity>
</Product>

and my code

string path = "Product/Name";
string name = xml.XPathSelectElement(path).Value;

note my string is coming from elsewhere so I guess it doesn't have to be literal ( at least in debug mode it looks like the one above). I've also tried adding / in front. It gives me a null ref.

like image 771
Ingó Vals Avatar asked Aug 11 '10 13:08

Ingó Vals


People also ask

How do I select a specific node in XML?

Select XML Nodes by Name [C#] To find nodes in an XML file you can use XPath expressions. Method XmlNode. SelectNodes returns a list of nodes selected by the XPath string. Method XmlNode.

Does Linq work with XML?

The most important advantage of LINQ to XML is its integration with Language-Integrated Query (LINQ). This integration enables you to write queries on the in-memory XML document to retrieve collections of elements and attributes.

What is XPath in XML example?

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.


2 Answers

Try using the XPathSelectElement extension method of XElement. You can pass the method an XPath expression to evaluate. For example:

XElement myElement = rootElement.XPathSelectElement("//Book[@ISBN='22542']");

Edit:

In reply to your edit, check your XPath expression. If your document only contains that small snippet then /Product/Name will work as the leading slash performs a search from the root of the document:

XElement element = document.XPathSelectElement("/Product/Name");

If there are other products and <Product> is not the root node you'll need to modify the XPath you're using.

like image 59
Matt B Avatar answered Oct 09 '22 07:10

Matt B


You can also use XPathEvaluate

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

Given the following xml:

<?xml version="1.0" encoding="utf-8" ?>
<documents>
  <items>
    <item name="Jamie"></item>
    <item name="John"></item>
  </items>
</documents>

This should print the contents from the items node.

like image 32
stephenlloyd Avatar answered Oct 09 '22 08:10

stephenlloyd