473,320 Members | 1,945 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,320 software developers and data experts.

Path in XML

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 following:
testlayer
testlayer/text1
testlayer/text1/text11
testlayer/text1/text112
testlayer/text2
and so on.

How can i get with XMLNode objects in .Net Framework? or using another
objects?
Thanks, Cheers.

*** Xml ***
<?xml version="1.0" encoding="UTF-8"?>
<testlayer>
<text1>test1
<text11>test11</text11>
<text112>test112</text112>
</text1>
<text12>test12</text12>
<text2>Text test2</text2>
<text22>Text test22</text22>
<text3>Text test3</text3>
<text32>Text test32</text32>
</testlayer>
Nov 12 '05 #1
2 1589
Piculo wrote:
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 following:
testlayer
testlayer/text1
testlayer/text1/text11
testlayer/text1/text112
testlayer/text2
and so on.

How can i get with XMLNode objects in .Net Framework? or using another
objects?


Simple XSLT stylesheet can do it:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="*">
<xsl:for-each select="ancestor-or-self::*">
<xsl:text>/</xsl:text>
<xsl:value-of select="name()"/>
</xsl:for-each>
<xsl:text>&#xA;</xsl:text>
<xsl:apply-templates select="*"/>
</xsl:template>
</xsl:stylesheet>

You can improve it to add support for numeric predicates when there is
several namesake siblings elements etc, see
http://www.dpawson.co.uk/xsl/sect2/N6077.html.
The perf can also be improved using breadth--first traversal.

--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com
Nov 12 '05 #2
Han
You may want a recursive function.

static void Main(string[] args)
{
XmlDocument doc=new XmlDocument();
doc.Load("c:/x.xml");
XmlNode n=doc.DocumentElement;
string sn=n.Name;
recur(n, sn);

Console.Read();
}

private static void recur(XmlNode xn, string sn)
{
Console.WriteLine(sn);

foreach (XmlNode n in xn.SelectNodes("*") )
{
Class1.recur(n, sn + "/" + n.Name);
}
}

"Piculo" <PakPiculo(NoSPam.at)gmail.com> wrote in message
news:ek**************@TK2MSFTNGP14.phx.gbl...
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 following:
testlayer
testlayer/text1
testlayer/text1/text11
testlayer/text1/text112
testlayer/text2
and so on.

How can i get with XMLNode objects in .Net Framework? or using another
objects?
Thanks, Cheers.

*** Xml ***
<?xml version="1.0" encoding="UTF-8"?>
<testlayer>
<text1>test1
<text11>test11</text11>
<text112>test112</text112>
</text1>
<text12>test12</text12>
<text2>Text test2</text2>
<text22>Text test22</text22>
<text3>Text test3</text3>
<text32>Text test32</text32>
</testlayer>

Nov 12 '05 #3

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

Similar topics

31
by: John Roth | last post by:
I'm adding a thread for comments on Gerrit Holl's pre-pep, which can be found here: http://tinyurl.com/2578q Frankly, I like the idea. It's about time that all of the file and directory stuff...
9
by: Bengt dePaulis | last post by:
I have a local directory that I want to include in my sys.path How to save it permanently? Regards /Bengt
70
by: Michael Hoffman | last post by:
Many of you are familiar with Jason Orendorff's path module <http://www.jorendorff.com/articles/python/path/>, which is frequently recommended here on c.l.p. I submitted an RFE to add it to the...
34
by: Reinhold Birkenfeld | last post by:
Hi, the arguments in the previous thread were convincing enough, so I made the Path class inherit from str/unicode again. It still can be found in CVS:...
2
by: Rob Cowie | last post by:
Hi, Given a string representing the path to a file, what is the best way to get at the filename? Does the OS module provide a function to parse the path? or is it acceptable to split the string...
1
by: William Stacey [MVP] | last post by:
I need a bullet proof way to combine a root and a relative path to form a FQ rooted path (similar to a VDir in IIS). Path.Combine alone will not do the job in all cases. I also need to be sure...
4
by: python | last post by:
Bad file names, i.e. filenames the OS considers illegal, will cause functions in the os.path module to raise an error. Example: import os.path print os.path.getsize( 'c:/pytest/*.py' ) On...
34
by: Alexnb | last post by:
Gerhard Häring wrote: No, it didn't work, but it gave me some interesting feedback when I ran it in the shell. Heres what it told me: Traceback (most recent call last): File...
8
by: moondaddy | last post by:
I'm posting code for a user control ( FunctionConnectorSelector) below which has 3 content controls in it. each content control uses a style from a resource dictionary merged into the app.xaml...
6
by: bukzor | last post by:
I have to go into these convulsions to get the directory that the script is in whenever I need to use relative paths. I was wondering if you guys have a better way: from os.path import dirname,...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.