static string GetXPath(XElement node)
{
StringBuilder builder = new StringBuilder();
while (node != null)
{
switch (node.NodeType)
{
case XmlNodeType.Element:
int index = GetElementIndex(node);
builder.Insert(0, "/" + node.Name + "[" + index + "]");
node = node.Parent;
break;
case XmlNodeType.Document:
return builder.ToString();
default:
throw new ArgumentException("Only elements and attributes are supported");
}
}
throw new ArgumentException("Node was not in a document");
}