Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to get Full XML node name using C#

Posted on 2009-05-18
4
Medium Priority
?
1,383 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 1000 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 1000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

876 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