How to get xpath from an XmlNode instance

后端 未结 14 989
难免孤独
难免孤独 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;
        }
    
    0 讨论(0)
  • 2020-11-30 19:13
     public static string GetFullPath(this XmlNode node)
            {
                if (node.ParentNode == null)
                {
                    return "";
                }
                else
                {
                    return $"{GetFullPath(node.ParentNode)}\\{node.ParentNode.Name}";
                }
            }
    
    0 讨论(0)
提交回复
热议问题