XmlNode and XPath

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

    #1

    XmlNode and XPath

    Hi All,

    I noticed that XmlNode and XpathNavigator are quite
    similiar. XmlNode seems to navigate over an XML Doc and
    so does XPathNav so when do I use XPathNavigator instead
    of XmlNode?

    Thanks,

    JJ
  • Oleg Tkachenko

    #2
    Re: XmlNode and XPath

    JJ wrote:
    [color=blue]
    > I noticed that XmlNode and XpathNavigator are quite
    > similiar. XmlNode seems to navigate over an XML Doc and
    > so does XPathNav so when do I use XPathNavigator instead
    > of XmlNode?[/color]

    XPathNavigator is unified XPath selection facility. It allows navigate
    over any XPathNavigable stores, including XmlDocument. In fact
    SelectNode method's using XPathNavigator within.
    Hint: XmlDocument ios not only XML API in .NET, learn about
    XPathDocument, it's gonna shadow XmlDocument soon.
    --
    Oleg Tkachenko
    XML Insider


    Comment

    • JJ

      #3
      Re: XmlNode and XPath

      I am trying to work with XPathDocument in my code but I
      tryed to assign it like the following and get an error
      that it can't cast to a XmlNode. Here's the code:

      XPathDocument xd = new
      XPathDocument(@ "D:\XML\XSL Ex2\Personnel.x ml");

      XPathNavigator nav = xd.CreateNaviga tor();

      XslTransform xslt = new XslTransform();
      xslt.Load(@"D:\ XML\XSL Ex2\Personnel2. xsl");
      StringWriter fs = new StringWriter();
      xslt.Transform( xd, null, fs, null);

      //select the root node
      nav.MoveToRoot( );


      XmlNode node = nav.Select("Emp s"); <- It errors
      here

      What do u think Oleg?

      JJ

      [color=blue]
      >-----Original Message-----
      >JJ wrote:
      >[color=green]
      >> I noticed that XmlNode and XpathNavigator are quite
      >> similiar. XmlNode seems to navigate over an XML Doc[/color][/color]
      and[color=blue][color=green]
      >> so does XPathNav so when do I use XPathNavigator[/color][/color]
      instead[color=blue][color=green]
      >> of XmlNode?[/color]
      >
      >XPathNavigat or is unified XPath selection facility. It[/color]
      allows navigate[color=blue]
      >over any XPathNavigable stores, including XmlDocument.[/color]
      In fact[color=blue]
      >SelectNode method's using XPathNavigator within.
      >Hint: XmlDocument ios not only XML API in .NET, learn[/color]
      about[color=blue]
      >XPathDocumen t, it's gonna shadow XmlDocument soon.
      >--
      >Oleg Tkachenko
      >XML Insider
      >http://www.tkachenko.com/blog
      >
      >.
      >[/color]

      Comment

      • Oleg Tkachenko

        #4
        Re: XmlNode and XPath

        JJ wrote:
        [color=blue]
        > I am trying to work with XPathDocument in my code but I
        > tryed to assign it like the following and get an error
        > that it can't cast to a XmlNode. Here's the code:[/color]
        XPathDocument is read-only, it's XML querying facility. If you need
        editing - use XmlDocument.
        At least till .NET 1.2.
        --
        Oleg Tkachenko
        XML Insider


        Comment

        • JJ

          #5
          XmlNode and XPath

          Oleg,

          Here is the code of an XmlNode :

          XmlNode node;
          if(node.Attribu tes.Count>0){}

          Using XPathNav what is its equivalent?

          Thanks,

          JJ




          [color=blue]
          >-----Original Message-----
          >Hi All,
          >
          > I noticed that XmlNode and XpathNavigator are quite
          >similiar. XmlNode seems to navigate over an XML Doc and
          >so does XPathNav so when do I use XPathNavigator instead
          >of XmlNode?
          >
          >Thanks,
          >
          >JJ
          >.
          >[/color]

          Comment

          • JJ

            #6
            XmlNode and XPath

            Oleg,

            In your reply about the casting error, all I was trying
            to do was assign a selected node from XpathNav to an
            XmlNode, not vise versa, so I don't quite understand why
            it failed?

            - XPathNodeIterat or Question -
            Also I noticed that in my readings that when I use
            XPathNavigator, I see mentioning of using a
            XPathNodeIterat or :

            XPathNodeIterat or ni = nav.Select(expr )

            This seems to allow me to go and query though a selected
            set of Nodes.

            So I should also be using this to get at any particular
            node?

            Thanks,

            JJ

            [color=blue]
            >-----Original Message-----
            >Hi All,
            >
            > I noticed that XmlNode and XpathNavigator are quite
            >similiar. XmlNode seems to navigate over an XML Doc and
            >so does XPathNav so when do I use XPathNavigator instead
            >of XmlNode?
            >
            >Thanks,
            >
            >JJ
            >.
            >[/color]

            Comment

            • JJ

              #7
              XmlNode and XPath

              One more thing Oleg,

              I not sure but I thought in order to use XSLTranforms,
              I needed to use an XpathDocument? If I can use
              XMLDocument instead and attach an XSLT sheet that would
              be great too. If I can use XSLT with XMLDocument what is
              the command to assign it to xmldocument?

              Thanks for all your Help!!!

              JJ


              [color=blue]
              >-----Original Message-----
              >Hi All,
              >
              > I noticed that XmlNode and XpathNavigator are quite
              >similiar. XmlNode seems to navigate over an XML Doc and
              >so does XPathNav so when do I use XPathNavigator instead
              >of XmlNode?
              >
              >Thanks,
              >
              >JJ
              >.
              >[/color]

              Comment

              • Oleg Tkachenko

                #8
                Re: XmlNode and XPath

                JJ wrote:[color=blue]
                > I not sure but I thought in order to use XSLTranforms,
                > I needed to use an XpathDocument? If I can use
                > XMLDocument instead and attach an XSLT sheet that would
                > be great too.[/color]

                You can use both XmlDocument and XPpathDocument as input for
                transformation, but beware XPathDocument is faster as it's optimized
                exactly for XPath and is read-only. Use XPathDocument if you don't need
                to edit XML in-memory.
                [color=blue]
                > If I can use XSLT with XMLDocument what is
                > the command to assign it to xmldocument?[/color]
                There is no such command. Instead take a look at
                XslTransform.Tr ansform() method.
                --
                Oleg Tkachenko

                Multiconn Technologies, Israel

                Comment

                • Oleg Tkachenko

                  #9
                  Re: XmlNode and XPath

                  JJ wrote:
                  [color=blue]
                  > Here is the code of an XmlNode :
                  >
                  > XmlNode node;
                  > if(node.Attribu tes.Count>0){}
                  >
                  > Using XPathNav what is its equivalent?[/color]

                  if (nav.MoveToFirs tAttribute()) {
                  ...
                  //Get back to element
                  nav.MoveToParen t();
                  ...
                  }

                  XPathNavigator is cursor-like API, which should be familiar for DBMS
                  oriented heads. Using it you are *navigating* over XML tree.
                  --
                  Oleg Tkachenko
                  XML Insider


                  Comment

                  • Oleg Tkachenko

                    #10
                    Re: XmlNode and XPath

                    JJ wrote:
                    [color=blue]
                    > This seems to allow me to go and query though a selected
                    > set of Nodes.
                    >
                    > So I should also be using this to get at any particular
                    > node?[/color]

                    Sure. But only if you are navigating over XmlDocument.
                    To get selected XmlNode you should use IHasXmlNode interface:
                    XmlNode node = ((IHasXmlNode)n i.Current).GetN ode();
                    --
                    Oleg Tkachenko
                    XML Insider


                    Comment

                    Working...