Path of Current Node in XDocument?

There's nothing built in, but you could write your own extension method: public static string GetPath(this XElement node) { string path = node.Name.ToString(); XElement currentNode = node; while (currentNode. Parent! = null) { currentNode = currentNode.

Parent; path = currentNode.Name.ToString() + @"\" + path; } return path; } XElement node = .. string path = node.GetPath() This doesn't account for the position of the element within its peer group though.

There's nothing built in, but you could write your own extension method: public static string GetPath(this XElement node) { string path = node.Name.ToString(); XElement currentNode = node; while (currentNode. Parent! = null) { currentNode = currentNode.

Parent; path = currentNode.Name.ToString() + @"\" + path; } return path; } XElement node = .. string path = node.GetPath(); This doesn't account for the position of the element within its peer group though.

1 Don't forget that such path also needs proper namespace handling... – Alexei Levenkov May 5 at 21:31.

Depending on how you want to use the XPath. In either case you'll need to walk tree up yourself and build XPath on the way. If you want to have readable string for dispaly - just joining names of prent nodes (see BrokenGlass suggestion) works fine If you want select later on the XPath positional XPath (specify position of each node in its parent) is one option (something like //3/*).

You need to consider attributes as special case as there is no order defined on attributes XPath with pre-defined prefixes (namespace to prefix need to be stored separately) - /my:root/my:child3/o:prop1/@t:attr3 XPath with inlined namespaces when you want semi-readable and portable XPath /*name()='root' & namespace-uri()='http://my. Namespace'/.... (see specification for name and namespace-uri functions w3.org/TR/xpath/#function-namespace-uri) Note that special nodes like comments and processing instructions may need to be taken into account if you want truly generic version of the XPath to a node inside XML.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions