Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to get Full XML node name using C#

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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

688 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