07 March 2011

How to retrieve Xml node xPath with c#


 public static string GetXPath(this XmlNode node)
        {
            if (node.NodeType == XmlNodeType.Attribute)
            {
                // attributes have an OwnerElement, not a ParentNode; also they have
                // to be matched by name, not found by position
                return String.Format(
                    "{0}/@{1}",
                    GetXPath(((XmlAttribute)node).OwnerElement),
                    node.Name
                    );
            }

            if (node.ParentNode == null)
            {
                // the only node with no parent is the root node, which has no path
                return "";
            }

            //get the index of the node
            int iIndex = 1;
            XmlNode xnIndex = node;

            while (xnIndex.PreviousSibling != null)
            {
                iIndex++;
                xnIndex = xnIndex.PreviousSibling;
            }

            // the path to a node is the path to its parent, plus "/node()[n]", where
            // n is its position among its siblings.
            return String.Format(
                "{0}/{1}[{2}]",
                GetXPath(node.ParentNode),
                node.Name,
                iIndex
                );
        }

3 comments:

Anonymous said...

A fascinating discussion is definitely worth comment. I do believe that you ought to publish more about
this subject, it may not be a taboo matter but typically people don't talk about such topics. To the next! Many thanks!!

my webpage "Eleatic"

Anonymous said...

Heisse Teens ab 18 live hinter der eigenen sexcam.
Willst du mit ihnen chatten? Melde dich jetzt umsonnst an und du bekommst sofort 50 Coins für die live cams umsonnst!
Erlebe geile [b][url=http://free-teen.org]Teen Sex Cams[/url][/b]. Gerade erst 18 und schon so versaut.

[url=http://scforum.mi.demo.mirchevideas.com/memberlist.php?mode=viewprofile&u=35122]Teen Tube[/url]
[url=http://3dsound4games.com/forum/memberlist.php?mode=viewprofile&u=8566]Amateur Teens[/url]
[url=http://onemanarmies.com/memberlist.php?mode=viewprofile&u=11069]Teen Dating[/url]

[url=http://fm-unit.de/phpBB/memberlist.php?mode=viewprofile&u=6426]Teens ab 18[/url]
[url=http://southernstormchasing.com/asrc/forum/index.php?action=profile;u=34682]Teens ab 18[/url]
[url=http://showrunn.ru/all_about/adminskiy-sortir.html#comment-3476]Free Teens[/url]

http://peopleofcincy.com/memberlist.php?mode=viewprofile&u=3400 http://king.p2h.info/vb/member.php?u=3268 http://film.16mb.com/index.php?subaction=userinfo&user=CamTeeni webcam[/url]

Anonymous said...

Нey thеre, I found your sіte blоgger.

com ωhile brоwѕіng http://dotnеtbiblе.

blοgspot.cоm. Ηaνе yоu ever
thought аbοut makіng usе of ѕlightly morе than sіmply tеxt in your агticlеѕ?
І mеan, what you say is uѕeful anԁ аll.
Ηowevеr, I think that if уou integratеd some οther formѕ of
media (such as videoѕ, νіѕualѕ, ρictures) you might make уour pоѕts moгe substantiѵе аnd appealing.

Yоur material is awеsomе, but if you were to incоrpοratе more different media, yоur pаge could dеfinitely be one of the beѕt in its catеgorу in
contrast to other sites on buzzle.соm.

My blog ρost: 88437