473,387 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to get the XML path of an XmlNode?

How can I get the full XML path (as string) of a specific XmlNode ?
--
Thanks
Sharon
Nov 20 '06 #1
5 8330
Hi Sharon,

I'm not aware of anything out of the box that will give you this
information, but you can easily create the path using XmlNode.Parent

// given an XmlNode node

string s = node.Name;
while (node.ParentNode != null && node.ParentNode.NodeType !=
XmlNodeType.Document)
{
node = node.ParentNode;
s = node.Name + "/" + s;
}
On Mon, 20 Nov 2006 10:02:01 +0100, Sharon <Sh*****@newsgroups.nospam
wrote:
How can I get the full XML path (as string) of a specific XmlNode ?



--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 20 '06 #2
This is one of those examples where I would recommend a StringBuilder over a
string for building the path, especially if this code is going to be called
many times.

Mark.
--
http://www.markdawson.org
"Morten Wennevik" wrote:
Hi Sharon,

I'm not aware of anything out of the box that will give you this
information, but you can easily create the path using XmlNode.Parent

// given an XmlNode node

string s = node.Name;
while (node.ParentNode != null && node.ParentNode.NodeType !=
XmlNodeType.Document)
{
node = node.ParentNode;
s = node.Name + "/" + s;
}
On Mon, 20 Nov 2006 10:02:01 +0100, Sharon <Sh*****@newsgroups.nospam>
wrote:
How can I get the full XML path (as string) of a specific XmlNode ?

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 20 '06 #3
Agreed; using .Insert() (or Push() each to a Stack<XmlNodeand Pop() it
afterwards, using .Append()).

Note, however, that this approach will not guarantee a usable path; for
instance, it doesn't distingusish between siblings, and doesn't allow for
namespaces, attribs, etc.

If you want this type of functionality, often a better approach is to have
an ID attribute or similar, so that you can uniquely reference the elements.

Marc
Nov 20 '06 #4
I don't see how a StringBuilder would improve this functionality. Unless
the xml is incredibly deep (>100 subnodes) there really won't be much, or
any, improvement. If you call the code many times you won't benefit from
a StringBuilder if you have to create the StringBuilder object each time.
On Mon, 20 Nov 2006 10:46:01 +0100, Mark R. Dawson
<Ma*********@discussions.microsoft.comwrote:
This is one of those examples where I would recommend a StringBuilder
over a
string for building the path, especially if this code is going to be
called
many times.

Mark.


--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 20 '06 #5
This is one of those examples where I would recommend a StringBuilder over a
string for building the path, especially if this code is going to be called
many times.

Mark.
My guess is that in this case it will take *longer* if you use a
StringBuilder, because the new value is added at the *beginning*.
I imagine that adding at the front of the string will involve shifting
the old string around, which might take more time that the simple
string concatenation of the original solution.

But of course the best way to decide would be to time the various
solutions, using reasonable test-data (as others said, the node will
probably not be too deep).
Hans Kesting
>
"Morten Wennevik" wrote:
>Hi Sharon,

I'm not aware of anything out of the box that will give you this
information, but you can easily create the path using XmlNode.Parent

// given an XmlNode node

string s = node.Name;
while (node.ParentNode != null && node.ParentNode.NodeType !=
XmlNodeType.Document)
{
node = node.ParentNode;
s = node.Name + "/" + s;
}

On Mon, 20 Nov 2006 10:02:01 +0100, Sharon <Sh*****@newsgroups.nospam>
wrote:
>>How can I get the full XML path (as string) of a specific XmlNode ?

--
Happy Coding!
Morten Wennevik [C# MVP]

Nov 20 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: JJ | last post by:
Hi All, I noticed that XmlNode and XpathNavigator are quite similiar. XmlNode seems to navigate over an XML Doc and so does XPathNav so when do I use XPathNavigator instead of XmlNode? ...
4
by: The Clansman | last post by:
Hi, how do I get the path of an XML node? I have: Dim xmlNo As XmlNode = xml.SelectSingleNode("//Category") I want to get the path of the xmlNo: i.e.
3
by: Mahesh Devjibhai Dhola | last post by:
Hi All, I want to make a custom class in c#, which extends System.Xml.XmlNode class of BCL. Now in custom class, I have implement abstract methods of XmlNode class also. Now when I am trying to...
2
by: Piculo | last post by:
Hi, I am working with XMLDocuments, and i go through it with XMLNode, and i want know for each node the path in the XMlDocuments. For instance for this XML i want get for each nodes the...
1
by: andrej | last post by:
hi, ich habe eine anwendung, welche ein xml document erstellt. um festzustellen, ob ein element bereits vorhanden ist, verwende ich die funktion selectsinglenode( ....) diese funktion...
5
by: Mahesh Devjibhai Dhola | last post by:
Hi All, I want to make a custom class in c#, which extends System.Xml.XmlNode class of BCL. Now in custom class, I have implement abstract methods of XmlNode class also. Now when I am trying to...
3
by: Mae | last post by:
Dear All, I have a problem here, I'm using C# Webform calling a webservices. The webservices return me a XMLnode, using this XMLnode I want to convert it to dataset so I can bind to the...
2
by: Christian Rühl | last post by:
heyho, guys! here's another question for you now: i built an iterator to get all the nodes with a certain attribute in an xml dom. it all looks like this (i'm using .NET framework 1.1) ...
7
by: Sharon | last post by:
How can I get the full XML path (as string) of a specific XmlNode ? -- Thanks Sharon
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.