System.Xml.XPath function

Hi,

The XPath expression "//Employee/Name" is given to the Select() method and it returns a set of nodes. Now the expression is a general one. Name at any level. Is there a method in System.Xml.XPath namespace which gives the absolute path of the resultant expression. Say if it has two results then I should be able to determine
/root/level1/Employee/Name and /root/level1/level2/level3/Employee/Name.

Is this possible



Answer this question

System.Xml.XPath function

  • anam

    Hi,

    Check this out. I used SelectAncestors().This one worked for me.

    XPathDocument doc = new
    XPathDocument("Sample.xml");
    XPathNavigator nav = doc.CreateNavigator();
    XPathNodeIterator iter =
    nav.Select("//Name(.,'John')]");
    string path;
    while(iter.MoveNext())
    {
    XPathNavigator nav1 = iter.Current;
    XPathNodeIterator ni =
    nav1.SelectAncestors(XPathNodeType.Element,true);
    path = ni.Current.Name;
    path = string.Concat("/",path);
    ni.MoveNext();
    while(ni.MoveNext())
    path = string.Concat("/",ni.Current.Name,path);
    Console.WriteLine(path);
    path = "";
    }

  • System.Xml.XPath function