How to get xpath from an XmlNode instance

后端 未结 14 1071
难免孤独
难免孤独 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 18:47

    What about using class extension ? ;) My version (building on others work) uses the syntaxe name[index]... with index omited is element has no "brothers". The loop to get the element index is outside in an independant routine (also a class extension).

    Just past the following in any utility class (or in the main Program class)

    static public int GetRank( this XmlNode node )
    {
        // return 0 if unique, else return position 1...n in siblings with same name
        try
        {
            if( node is XmlElement ) 
            {
                int rank = 1;
                bool alone = true, found = false;
    
                foreach( XmlNode n in node.ParentNode.ChildNodes )
                    if( n.Name == node.Name ) // sibling with same name
                    {
                        if( n.Equals(node) )
                        {
                            if( ! alone ) return rank; // no need to continue
                            found = true;
                        }
                        else
                        {
                            if( found ) return rank; // no need to continue
                            alone = false;
                            rank++;
                        }
                    }
    
            }
        }
        catch{}
        return 0;
    }
    
    static public string GetXPath( this XmlNode node )
    {
        try
        {
            if( node is XmlAttribute )
                return String.Format( "{0}/@{1}", (node as XmlAttribute).OwnerElement.GetXPath(), node.Name );
    
            if( node is XmlText || node is XmlCDataSection )
                return node.ParentNode.GetXPath();
    
            if( node.ParentNode == null )   // the only node with no parent is the root node, which has no path
                return "";
    
            int rank = node.GetRank();
            if( rank == 0 ) return String.Format( "{0}/{1}",        node.ParentNode.GetXPath(), node.Name );
            else            return String.Format( "{0}/{1}[{2}]",   node.ParentNode.GetXPath(), node.Name, rank );
        }
        catch{}
        return "";
    }   
    

提交回复
热议问题