XmlTextReader - how find a particular node

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

    XmlTextReader - how find a particular node

    I am reading an xml file from a URL. I was originally doing this using the
    XmlDocument class. But this was very slow. The XmlTextReader is meant to be
    much quicker for forward only retrieval of data.

    I need to somehow access a particular list of nodes. e.g. I want to loop
    through all the nodes called <item></item>. How is this possible using
    XmlTextReader?
    I can see that you can get the .Value property of XmlTextReader. But I want
    to be able to specify the node I want to look at.

    e.g.

    <Group>
    <Items>
    <Item>content here</Item>
    <Item>some more content here</Item>
    <Items>
    <Activity></Activity>
    <Location></Location>
    </Group>

    thanks.

    CR
  • Marc Gravell

    #2
    Re: XmlTextReader - how find a particular node

    you can use ReadToFollowing ("Item") to jump to each Item - however, you need
    to be careful, as the "intuitive" code might be (IIRC) wrong:

    while(reader.Re adToFollowing(" Item")) {
    string content = reader.ReadElem entContentAsStr ing();
    // do something with content
    }

    The reason this is wrong is that ReadElementCont entAsString progresses
    /past/ the [Item] element, so the next call to ReadToFollowing () might skip
    the next row; you might be able-to do something with a sub-reader, else you
    might need to check (after calling ReadElementCont entAsString) if you are on
    an Item element; if you are, it is the *next* one, so read it; if you
    aren't, then ReadToFollowing

    Marc

    "CodeRazor" <CodeRazor@disc ussions.microso ft.com> wrote in message
    news:C1FA6EED-1E92-41BD-8F50-0B07182D27FA@mi crosoft.com...[color=blue]
    >I am reading an xml file from a URL. I was originally doing this using the
    > XmlDocument class. But this was very slow. The XmlTextReader is meant to
    > be
    > much quicker for forward only retrieval of data.
    >
    > I need to somehow access a particular list of nodes. e.g. I want to loop
    > through all the nodes called <item></item>. How is this possible using
    > XmlTextReader?
    > I can see that you can get the .Value property of XmlTextReader. But I
    > want
    > to be able to specify the node I want to look at.
    >
    > e.g.
    >
    > <Group>
    > <Items>
    > <Item>content here</Item>
    > <Item>some more content here</Item>
    > <Items>
    > <Activity></Activity>
    > <Location></Location>
    > </Group>
    >
    > thanks.
    >
    > CR[/color]


    Comment

    • Michael Nemtsev

      #3
      Re: XmlTextReader - how find a particular node

      Hello CodeRazor,



      use .Read() methods of XmlTextReader and then in switch (reader.NodeTyp e)
      check the case XmlNodeType.Ele ment u want to find


      C> I am reading an xml file from a URL. I was originally doing this
      C> using the XmlDocument class. But this was very slow. The
      C> XmlTextReader is meant to be much quicker for forward only retrieval
      C> of data.
      C>
      C> I need to somehow access a particular list of nodes. e.g. I want to
      C> loop
      C> through all the nodes called <item></item>. How is this possible
      C> using
      C> XmlTextReader?
      C> I can see that you can get the .Value property of XmlTextReader. But
      C> I want
      C> to be able to specify the node I want to look at.
      C> e.g.
      C>
      C> <Group>
      C> <Items>
      C> <Item>content here</Item>
      C> <Item>some more content here</Item>
      C> <Items>
      C> <Activity></Activity>
      C> <Location></Location>
      C> </Group>
      C> thanks.
      C>
      C> CR
      C>
      ---
      WBR,
      Michael Nemtsev :: blog: http://spaces.msn.com/laflour

      "At times one remains faithful to a cause only because its opponents do not
      cease to be insipid." (c) Friedrich Nietzsche


      Comment

      • Marc Gravell

        #4
        Re: XmlTextReader - how find a particular node

        et voila (note your xml was malformed); also, generally (for large xml) I
        wouldn't use a string and MemoryStream here unless I had to - but it was the
        only 1-line way I could think of to initialise the reader. Loading directly
        from the uri (or a file) would be an option, though...

        Marc

        using System;
        using System.Xml;
        using System.Text;
        using System.IO;
        namespace ReadSomeXml {
        class Program {
        static void Main(string[] args)
        {
        string xml = @"<Group>
        <Items>
        <Item>content here</Item>
        <Item>some more content here</Item>
        </Items>
        <Activity></Activity>
        <Location></Location>
        </Group>";
        using(XmlReader reader = XmlReader.Creat e(new
        MemoryStream(En coding.UTF8.Get Bytes(xml)))) {

        while(true) {
        if(reader.NodeT ype == XmlNodeType.Ele ment && reader.Name
        == "Item") {
        string content =
        reader.ReadElem entContentAsStr ing();
        Console.WriteLi ne(content);
        } else {
        if(!reader.Read ToFollowing("It em")) break;
        }
        }
        }
        }
        }
        }
        "Marc Gravell" <marc@nonesuch. com> wrote in message
        news:eN47sAsRGH A.3192@TK2MSFTN GP09.phx.gbl...[color=blue]
        > you can use ReadToFollowing ("Item") to jump to each Item - however, you
        > need to be careful, as the "intuitive" code might be (IIRC) wrong:
        >
        > while(reader.Re adToFollowing(" Item")) {
        > string content = reader.ReadElem entContentAsStr ing();
        > // do something with content
        > }
        >
        > The reason this is wrong is that ReadElementCont entAsString progresses
        > /past/ the [Item] element, so the next call to ReadToFollowing () might
        > skip the next row; you might be able-to do something with a sub-reader,
        > else you might need to check (after calling ReadElementCont entAsString) if
        > you are on an Item element; if you are, it is the *next* one, so read it;
        > if you aren't, then ReadToFollowing
        >
        > Marc
        >
        > "CodeRazor" <CodeRazor@disc ussions.microso ft.com> wrote in message
        > news:C1FA6EED-1E92-41BD-8F50-0B07182D27FA@mi crosoft.com...[color=green]
        >>I am reading an xml file from a URL. I was originally doing this using
        >>the
        >> XmlDocument class. But this was very slow. The XmlTextReader is meant to
        >> be
        >> much quicker for forward only retrieval of data.
        >>
        >> I need to somehow access a particular list of nodes. e.g. I want to loop
        >> through all the nodes called <item></item>. How is this possible using
        >> XmlTextReader?
        >> I can see that you can get the .Value property of XmlTextReader. But I
        >> want
        >> to be able to specify the node I want to look at.
        >>
        >> e.g.
        >>
        >> <Group>
        >> <Items>
        >> <Item>content here</Item>
        >> <Item>some more content here</Item>
        >> <Items>
        >> <Activity></Activity>
        >> <Location></Location>
        >> </Group>
        >>
        >> thanks.
        >>
        >> CR[/color]
        >
        >[/color]


        Comment

        • CodeRazor

          #5
          Re: XmlTextReader - how find a particular node

          Thanks for your help, but I am still stumped.

          I have found an inelegant way of retrieving each item's title and
          description using string methods. See my code below. I would like to know how
          I can retrieve the <title> and <description> nodes more directly using the
          methods of the XmlTextReader.

          My xml file looks somthing like:
          <items>
          <item><title>Ti tle 1</title><descript ion>Description 1</description></item>
          <item><title>Ti tle 2</title><descript ion>Description 2</description></item>
          </items>

          My Hack:
          XmlTextReader xmlTextReader = new XmlTextReader(m yXmlUrl);

          while(xmlTextRe ader.Read())
          {
          if(xmlTextReade r.NodeType == XmlNodeType.Ele ment && xmlTextReader.N ame ==
          "item" )
          {
          string itemContent = xmlTextReader.R eadOuterXml();
          int headlineStart = itemContent.Ind exOf("<title>") + "<title>".Lengt h;
          headlineLength = itemContent.Ind exOf("</title>") - headlineStart;
          descriptStart = itemContent.Ind exOf("<descript ion>") +
          "<description>" .Length;
          int descriptLength = itemContent.Ind exOf("</description>") - descriptStart;
          string headline = itemContent.Sub string(headline Start,
          headlineLength) ;
          string description = itemContent.Sub string(descript Start, descriptLength) ;

          Response.Write( headline + "<br>" + description + "<br><br>") ;
          }

          Many thanks,

          CR

          Comment

          Working...