Getting the path of a xml node

I needed a function that would give the path of an xml node to the root.

This is the result that I came up with:

private string XmlNodePath(XmlNode node) {

  List<string> list = new List<string>();

  XmlNode workNode = node;

  do {

    if (workNode.Name != “#text”) {

      list.Insert(0, workNode.Name);

    }

    workNode = workNode.ParentNode;

  } while (workNode.NodeType == XmlNodeType.Element);

 

  return string.Join(“/”, list.ToArray());

}

Comments are closed.