arraylist & path

  • Thread starter Thread starter C# newbie
  • Start date Start date
C

C# newbie

Hi All,

Long story short, what is the best way to keep track of the path within an
XML file when running a query (XPath) for search and finding an item in xml
file and hold the path into an object like Arraylist?


Thanks
Newbie
 
the story's so short I don't understand it. what do you mean by "when
running a query" - are you using SelectNodes()?
 
Yes exactly, I need to know how to capture and traversed path into an xml
file after running an xpath query.

any suggestions ?
Thanks
 
I think I understand now - the idea I came up with is:

static void Main(string[] args)
{
XmlDocument x = new XmlDocument();
x.LoadXml("<a><b/><b><c/></b></a>");
XmlNode n = x.SelectSingleNode("a/b/c");
string xpath = get_xpath(n);
}
static string get_xpath(XmlNode n)
{
if(n.ParentNode == null) return "/";
int order = n.SelectNodes("preceding-sibling::"+n.Name).Count + 1;
return string.Format("{0}/{1}[{2}]", get_xpath(n.ParentNode), n.Name,
order);
}

Maybe there's a more direct and efficient way, but I can't think of one
right now.
 
Back
Top