How to get the XML path of an XmlNode?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sharon

    How to get the XML path of an XmlNode?

    How can I get the full XML path (as string) of a specific XmlNode ?


    --
    Thanks
    Sharon
  • Martin Honnen

    #2
    Re: How to get the XML path of an XmlNode?

    Sharon wrote:
    How can I get the full XML path (as string) of a specific XmlNode ?
    What is "the full XML Path" of a node, do you have any definition for
    that? Some XML editors can assist you constructing or maybe even
    generating an XPath expression.


    --

    Martin Honnen --- MVP XML

    Comment

    • Sharon

      #3
      Re: How to get the XML path of an XmlNode?

      Lets say I have the following XML file:

      <?xml version="1.0" encoding="utf.8 "?>
      <System Name = "System">
      <Parameters>
      <T1>
      <T2>
      <T3>
      <L3.1>111</L3.1>
      <L3.1>222</L3.1>
      <L3.1>333</L3.1>
      <L3.1>444</L3.1>
      <L3.1>555</L3.1>
      </T3>
      </T2>
      </T1>
      </Parameters>
      </System>


      And in some point in the code, someone does:
      XmlDocument doc = new XmlDocument();
      doc.LoadXml( XmlFilenAme );
      XmlNode xNode = doc.SelectSingl eNode("//System/Parameters/T1/T2/T3/L3.1");

      And passes this xNode to me in some other piece of code.

      Now I need to know the full XML path of the given node, that in this case is
      the "//System/Parameters/T1/T2/T3/L3.1".



      So the question is: How can I figure out the full XML path of any given
      System.Xml.XmlN ode ?


      -----
      Thanks
      Sharon

      Comment

      • Dimitre Novatchev

        #4
        Re: How to get the XML path of an XmlNode?




        Cheers,
        Dimitre Novatchev


        "Sharon" <SharonG@newsgr oups.nospamwrot e in message
        news:521FBE3C-23C9-4E07-9332-ED0ED3F1800B@mi crosoft.com...
        Lets say I have the following XML file:
        >
        <?xml version="1.0" encoding="utf.8 "?>
        <System Name = "System">
        <Parameters>
        <T1>
        <T2>
        <T3>
        <L3.1>111</L3.1>
        <L3.1>222</L3.1>
        <L3.1>333</L3.1>
        <L3.1>444</L3.1>
        <L3.1>555</L3.1>
        </T3>
        </T2>
        </T1>
        </Parameters>
        </System>
        >
        >
        And in some point in the code, someone does:
        XmlDocument doc = new XmlDocument();
        doc.LoadXml( XmlFilenAme );
        XmlNode xNode = doc.SelectSingl eNode("//System/Parameters/T1/T2/T3/L3.1");
        >
        And passes this xNode to me in some other piece of code.
        >
        Now I need to know the full XML path of the given node, that in this case
        is
        the "//System/Parameters/T1/T2/T3/L3.1".
        >
        >
        >
        So the question is: How can I figure out the full XML path of any given
        System.Xml.XmlN ode ?
        >
        >
        -----
        Thanks
        Sharon

        Comment

        • Sharon

          #5
          Re: How to get the XML path of an XmlNode?

          Dimitre, thanks for your reply.
          After looking at the link you have posted, I'm afraid I don't understand it.
          In this link page I see only XML file. But I need to find the node XML path
          by code, like the C# using any standard parser.


          ------
          Thanks
          Sharon

          Comment

          • Gadget

            #6
            Re: How to get the XML path of an XmlNode?

            On Mon, 20 Nov 2006 07:27:02 -0800, Sharon wrote:
            Dimitre, thanks for your reply.
            After looking at the link you have posted, I'm afraid I don't understand it.
            In this link page I see only XML file. But I need to find the node XML path
            by code, like the C# using any standard parser.
            >
            >
            ------
            Thanks
            Sharon
            The problem you have is that it is very complex to identify the index of
            the elements in a node path, and that's what the example Dimittre posted is
            doing.
            An XmlNode has a ParentNode method, so you can just walk back up the tree,
            reading the name of each node, and that might give you something like
            System/Parameters/T1/T2/T3/L3.1
            The problem is that you also need the index of items that are part of a
            repeating node:
            System/Parameters/T1/T2/T3/L3.1[3]

            Your example says 'doc.SelectSing leNode', but there are 5 nodes in your
            document, so walking back up the tree will give you a simple XPath to the
            first node (if there are no repeating elements further up the tree), but
            this is a far from suitable solution.

            Why do you need the XPath expression? What is it being used for?

            Cheers,
            Gadget

            Comment

            • Dimitre Novatchev

              #7
              Re: How to get the XML path of an XmlNode?


              "Sharon" <SharonG@newsgr oups.nospamwrot e in message
              news:4283C7C2-F832-469F-B1A6-B3BAB09FA724@mi crosoft.com...
              Dimitre, thanks for your reply.
              After looking at the link you have posted, I'm afraid I don't understand
              it.
              In this link page I see only XML file. But I need to find the node XML
              path
              by code, like the C# using any standard parser.

              Sharon,

              The XSLT code is probably simpler than any C# code doing the same and much
              more portable.

              If I can say one thing to be remembered is that at present XSLT is the best
              programming language for tree manipulation/transformation.

              It is also not a bad functional programming language and has some very
              efficient implementations .


              Cheers,
              Dimitre Novatchev


              Comment

              • Peter Flynn

                #8
                Re: How to get the XML path of an XmlNode?

                Sharon wrote:
                How can I get the full XML path (as string) of a specific XmlNode ?
                Open the file in Emacs with psgml.
                Press C-c C-w

                That gives you the element descendancy.
                Change all occurrences of "in" to a slash to get a path.

                If you need the enumerated descendancy (as you imply by the use of the
                word "full"), install David Megginson's psgml-xpointer.el and type
                M-x sgml-xpointer RET

                That gives you the full (TEI-format) location ladder. I don't know of a
                function to return the XPath, but there's probably one out there. Most
                other decent editors will have similar functions.

                ///Peter
                --
                XML FAQ: http://xml.silmaril.ie/

                Comment

                Working...