Solved

How to get Full XML node name using C#

Posted on 2009-05-18
4
1,310 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now