Getting the xpath for a specific node in a xmldocument

  • Thread starter Thread starter Paw Pedersen
  • Start date Start date
P

Paw Pedersen

When you are "working" on a specific node from a XmlDocument instance, is it
possible to get the full xpath to this node?

Regards Paw
 
PAW,

I can not understand what you looking for but does a portion of the code
below address answer your question?


private const String localURL =
"http://localhost/quickstart/howto/samples/Xml/QueryXmlDocumentXPath/cs/book
s.xml";

public static void Main()
{
QueryXmlDocumentXPathSample myQueryXmlDocumentXPathSample = new
QueryXmlDocumentXPathSample();
myQueryXmlDocumentXPathSample.Run(localURL);
}

public void Run(String args)
{
Console.WriteLine("XPath Test started ...");

XPathDocument myXPathDocument = new XPathDocument(args);
XPathNavigator myXPathNavigator = myXPathDocument.CreateNavigator();

// Get all the book prices
XPathQuery(myXPathNavigator, "descendant::book/price");

// Get the ISBN of the last book
XPathQuery(myXPathNavigator, "bookstore/book[3]/@ISBN");
}

private void XPathQuery(XPathNavigator myXPathNavigator, String xpathexpr )
{
try
{
Console.WriteLine("XPath query: " + xpathexpr);

// Create a node interator to select nodes and move through them
(read-only)
XPathNodeIterator myXPathNodeIterator = myXPathNavigator.Select
(xpathexpr);

while (myXPathNodeIterator.MoveNext())
{
Console.WriteLine("<" + myXPathNodeIterator.Current.Name + "> "
+ myXPathNodeIterator.Current.Value);
}
Console.WriteLine();
}
catch (Exception e)
{
Console.WriteLine ("Exception: {0}", e.ToString());
}
}



--------------------
From: "Paw Pedersen" <[email protected]>
Subject: Getting the xpath for a specific node in a xmldocument
Date: Sun, 31 Oct 2004 16:59:25 +0100
Lines: 6
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.3790.181
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.181
Message-ID: <[email protected]>
Newsgroups: microsoft.public.dotnet.languages.csharp
NNTP-Posting-Host: cpe.atm2-0-1121065.0x503ebaea.arcnxx10.customer.tele.dk 80.62.186.234
cpmsftngxa10.phx.gbl!TK2MSFTNGXA03.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09
.phx.gbl
Xref: cpmsftngxa10.phx.gbl microsoft.public.dotnet.languages.csharp:283501
X-Tomcat-NG: microsoft.public.dotnet.languages.csharp

When you are "working" on a specific node from a XmlDocument instance, is it
possible to get the full xpath to this node?

Regards Paw

{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fprq2\fcharset0
MS Sans Serif;}{\f1\fswiss\fcharset0 Arial;}}
{\*\generator Msftedit 5.41.21.2500;}\viewkind4\uc1\pard\f0\fs20 Cheers,\par
\par
johnKn [MS-SDK]\par
\par
\par
\par
-Please do not send email directly to this alias. This alias is for \par
newsgroup purposes only\par
\par
-This posting is provided "AS IS" with no warranties, and confers no
rights.\par
\par
-To provide additional feedback about your community experience please send
\par
e-mail to: (e-mail address removed)\par
\f1\par
}
 
Why not write a function that walks the ancestors, checking each ancestor
the for the position in the collection and building a string from that
point?

That should be doable - then again, XPaths can be made i many different ways
but I guess you were looking for the fastest, right?
 
Thank you,
I will try that. I had just hoped there was a build in function for that.

Regards Paw
 
Hello!

I haven't seen such functionality from the core .NET framework, but that
doesn't necessarily mean it's not there =o)

I would google a little around or just roll the code. It's a nice little
project actually! Let's see what you come up with =o)
 
Back
Top