473,382 Members | 1,464 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,382 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
7 15289
Sharon wrote:
How can I get the full XML path (as string) of a specific XmlNode ?
What is "the full XML Path" of a node, do you have any definition for
that? Some XML editors can assist you constructing or maybe even
generating an XPath expression.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Nov 20 '06 #2
Lets say I have the following XML file:

<?xml version="1.0" encoding="utf.8"?>
<System Name = "System">
<Parameters>
<T1>
<T2>
<T3>
<L3.1>111</L3.1>
<L3.1>222</L3.1>
<L3.1>333</L3.1>
<L3.1>444</L3.1>
<L3.1>555</L3.1>
</T3>
</T2>
</T1>
</Parameters>
</System>
And in some point in the code, someone does:
XmlDocument doc = new XmlDocument();
doc.LoadXml( XmlFilenAme );
XmlNode xNode = doc.SelectSingleNode("//System/Parameters/T1/T2/T3/L3.1");

And passes this xNode to me in some other piece of code.

Now I need to know the full XML path of the given node, that in this case is
the "//System/Parameters/T1/T2/T3/L3.1".

So the question is: How can I figure out the full XML path of any given
System.Xml.XmlNode ?
-----
Thanks
Sharon
Nov 20 '06 #3
http://www.topxml.com/code/default.a...20010323001030
Cheers,
Dimitre Novatchev
"Sharon" <Sh*****@newsgroups.nospamwrote in message
news:52**********************************@microsof t.com...
Lets say I have the following XML file:

<?xml version="1.0" encoding="utf.8"?>
<System Name = "System">
<Parameters>
<T1>
<T2>
<T3>
<L3.1>111</L3.1>
<L3.1>222</L3.1>
<L3.1>333</L3.1>
<L3.1>444</L3.1>
<L3.1>555</L3.1>
</T3>
</T2>
</T1>
</Parameters>
</System>
And in some point in the code, someone does:
XmlDocument doc = new XmlDocument();
doc.LoadXml( XmlFilenAme );
XmlNode xNode = doc.SelectSingleNode("//System/Parameters/T1/T2/T3/L3.1");

And passes this xNode to me in some other piece of code.

Now I need to know the full XML path of the given node, that in this case
is
the "//System/Parameters/T1/T2/T3/L3.1".

So the question is: How can I figure out the full XML path of any given
System.Xml.XmlNode ?
-----
Thanks
Sharon

Nov 20 '06 #4
Dimitre, thanks for your reply.
After looking at the link you have posted, I'm afraid I don't understand it.
In this link page I see only XML file. But I need to find the node XML path
by code, like the C# using any standard parser.
------
Thanks
Sharon
Nov 20 '06 #5
On Mon, 20 Nov 2006 07:27:02 -0800, Sharon wrote:
Dimitre, thanks for your reply.
After looking at the link you have posted, I'm afraid I don't understand it.
In this link page I see only XML file. But I need to find the node XML path
by code, like the C# using any standard parser.
------
Thanks
Sharon
The problem you have is that it is very complex to identify the index of
the elements in a node path, and that's what the example Dimittre posted is
doing.
An XmlNode has a ParentNode method, so you can just walk back up the tree,
reading the name of each node, and that might give you something like
System/Parameters/T1/T2/T3/L3.1
The problem is that you also need the index of items that are part of a
repeating node:
System/Parameters/T1/T2/T3/L3.1[3]

Your example says 'doc.SelectSingleNode', but there are 5 nodes in your
document, so walking back up the tree will give you a simple XPath to the
first node (if there are no repeating elements further up the tree), but
this is a far from suitable solution.

Why do you need the XPath expression? What is it being used for?

Cheers,
Gadget
Nov 20 '06 #6

"Sharon" <Sh*****@newsgroups.nospamwrote in message
news:42**********************************@microsof t.com...
Dimitre, thanks for your reply.
After looking at the link you have posted, I'm afraid I don't understand
it.
In this link page I see only XML file. But I need to find the node XML
path
by code, like the C# using any standard parser.

Sharon,

The XSLT code is probably simpler than any C# code doing the same and much
more portable.

If I can say one thing to be remembered is that at present XSLT is the best
programming language for tree manipulation/transformation.

It is also not a bad functional programming language and has some very
efficient implementations.
Cheers,
Dimitre Novatchev
Nov 21 '06 #7
Sharon wrote:
How can I get the full XML path (as string) of a specific XmlNode ?
Open the file in Emacs with psgml.
Press C-c C-w

That gives you the element descendancy.
Change all occurrences of "in" to a slash to get a path.

If you need the enumerated descendancy (as you imply by the use of the
word "full"), install David Megginson's psgml-xpointer.el and type
M-x sgml-xpointer RET

That gives you the full (TEI-format) location ladder. I don't know of a
function to return the XPath, but there's probably one out there. Most
other decent editors will have similar functions.

///Peter
--
XML FAQ: http://xml.silmaril.ie/
Nov 26 '06 #8

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

Similar topics

7
by: Adam Smith | last post by:
How can I read binary data, specifically a microsoft word document into an XmlNode? Thanks in advance. Adam Smith
2
by: David Elliott | last post by:
I am creating a configuration class to read / write a standard configuration file. <?xml version="1.0" encoding="utf-8"?> <configuration> <appSettings> <add key="ConnectionString"...
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...
5
by: Paul | last post by:
Here I have the definition of an XmlNode which is a property (PayPreference) on my Customer class containing an enum describing how the customer will pay. <PayPerference...
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) ...
5
by: Sharon | last post by:
How can I get the full XML path (as string) of a specific XmlNode ? -- Thanks Sharon
5
by: jotaefe | last post by:
Hi everyone, After visiting and using plenty of your solutions exposed in this great website, I've finally got across a problem that I can't find a solution anywhere. I have a TreeView that is...
1
by: jesper_lofgren | last post by:
Hello, i need to help here. I have a xmlnode as you can se below (full xml is in the bottom). I wonder how i can get the values inside the CSammanstallningLista . <CSammanstallning...
5
by: Christof Nordiek | last post by:
When I load a XML-Document from a file, and then get an XmlNode from it, is there any way to get the its original position in the file (line, character)? I want to report it, when there is...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.