Solved

How to get Full XML node name using C#

Posted on 2009-05-18
4
1,344 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Telerik RadEditor Control Save 8 40
c#, case, if 4 39
How to calculate the sum of all rows at each new row in C# 4 61
What are MicroServices? 4 69
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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.
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

752 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