Solved

How to get Full XML node name using C#

Posted on 2009-05-18
4
1,325 Views
Last Modified: 2013-12-17
Suppose I have an XML file of the form
<aaa>
    <bbb>
       <ccc></ccc>
       <eee></eee>
     </bbb>
<aaa>

If I get an XMLNode (say eee), the name property will be eee;

but if I want to get the full path of the node ; like aaa | bbb | eee (the seperator can be customized later) is there  any way I can retrieve the FULL PATH STRING  from an XMLNode(like eee)?

0
Comment
Question by:ajaxdeveloper
  • 2
4 Comments
 
LVL 11

Accepted Solution

by:
saragani earned 250 total points
ID: 24418530
Well, it can be done by code, but what if you have:
<aaa>
    <bbb>
       <ccc></ccc>
       <eee></eee>
       <eee></eee>
       <eee></eee>
     </bbb>
<aaa>

You have several aaa.bbb.eee

That string is not unique... (Unlike full path in tree node).


XmlNode has a property called ParentNode
And each XmlNode has a Name.
So if you do a loop and create a string of the names, separated  by | or \ then you can have the full path
(You do the loop until ParentNode == Null)
0
 
LVL 3

Assisted Solution

by:channa_m
channa_m earned 250 total points
ID: 24423556
You can use this function
   public string GetFullPath(XmlNode xn)
    {
        string fullPath = "";
        System.Collections.Stack sta = new System.Collections.Stack();
        while (xn.ParentNode != null)
        {
            sta.Push(xn.Name);
            xn = xn.ParentNode;
        }
        while (sta.Count > 0)
        {
            string path = (string)sta.Pop();
            if (fullPath == "")
            {
                fullPath = path;
            }
            else
            {
                fullPath = fullPath + "|" + path;
            }
        }
        return fullPath;
    }

Open in new window

0
 
LVL 11

Expert Comment

by:saragani
ID: 24426053
channa_m  basically code my idea, but the only thing that I don't like is that he join strings using "+" other than using a StringBuilder which is a matter of resources.

I can't tell that string builder will always be faster than joining strings with +, but the thumb rule is that if the number of strings that you add to each other are 5 or more  then  use string builder.
If you don't know how many string you are going to join to each other then use string builder as well.
0
 

Author Comment

by:ajaxdeveloper
ID: 25039523
thanks a lot and sorry for late replies
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question