Re: SelectSingleNode in C#

From: Marc Scheuner [MVP ADSI] (m.scheuner_at_inova.SPAMBEGONE.ch)
Date: 03/09/04


Date: Tue, 09 Mar 2004 07:52:00 +0100


>Can someone show how to use SelectSingleNode and Xpath expression in C#?

First, you need an Xml Document which contains your XML string:

XmlDocument oXmlDoc = new XmlDocument();
oXmlDoc.LoadXml(<your XML string>);

Then you can get a specific, single node, by specifying a valid XPath
expression - read up on the details of those on the 'Net - tons of
good sites:

This sample would select the one (or the first) node <userdata>,
regardless of where in the Xml Document it's located:

XmlNode oTempNode = oXmlDoc.SelectSingleNode("//userdata");

This second example would select the single or first node <address>,
if it's located (directly) inside the <userdata> block - anywhere in
the document:

XmlNode oTempNode = oXmlDoc.SelectSingleNode("//userdata/address");

And this last sample would select the <city> node, directly inside the
<address> tag, which in turn is exactly under the <root> and the
<userdata> tags:

XmlNode oTempNode =
oXmlDoc.SelectSingleNode("root/userdata/address/city");

There's a ton of ways in which you can specify exactly what you're
looking for - that's the power of XPath, but I can't really give you
all the details here - read up on it, there's a ton of stuff out
there!

Marc
================================================================
Marc Scheuner May The Source Be With You!
Bern, Switzerland m.scheuner(at)inova.ch



Relevant Pages

  • Using XPath to copy nodes
    ... private Element getAnswers(Element root, String queID) throws ... XPath xpath = factory.newXPath; ... //Design the query to search through the XML Document ...
    (comp.lang.java.programmer)
  • Using XPath to copy nodes
    ... private Element getAnswers(Element root, String queID) throws ... XPath xpath = factory.newXPath; ... //Design the query to search through the XML Document ...
    (comp.lang.java.programmer)
  • Re: Using XPath Against A Node
    ... top parent? ... Or is the entire xml document sent with it. ... In terms of the XPath implementation with SelectSingleNode and SelectNodes if the node is not inserted in the owning document then it looks like the XPath / evaluates to the node itself. ...
    (microsoft.public.dotnet.languages.vb)
  • implementation of xpath in ruby?
    ... I have used token types to parse xml ... returns the tag end of an xml document. ... now i need to implement xpath in my parser class. ...
    (comp.lang.ruby)
  • [REVS] Blind XPath Injection
    ... The following security advisory is sent to the securiteam mailing list, and can be found at the SecuriTeam web site: http://www.securiteam.com ... The linked paper at the bottom describes a Blind XPath Injection attack ... that enables an attacker to extract a complete XML document used for XPath ...
    (Securiteam)