How to get xpath from an XmlNode instance

后端 未结 14 1116
难免孤独
难免孤独 2020-11-30 18:17

Could someone supply some code that would get the xpath of a System.Xml.XmlNode instance?

Thanks!

14条回答
  •  不知归路
    2020-11-30 19:12

    Here's a simple method that I've used, worked for me.

        static string GetXpath(XmlNode node)
        {
            if (node.Name == "#document")
                return String.Empty;
            return GetXpath(node.SelectSingleNode("..")) + "/" +  (node.NodeType == XmlNodeType.Attribute ? "@":String.Empty) + node.Name;
        }
    

提交回复
热议问题