Question regarding 70-528 --> XmlReader methods

Discussion in 'Microsoft Certification' started by Martin Eckart, Mar 18, 2007.

  1. Hi guys,

    I am about to try exam 70-528 and am stumbling about an issue with XmlReader
    methods Skip() and Read().

    Please see the following example:

    xml-file:
    <?xml version="1.0"?>
    <bookstore specialty="novel">
    <book style="autobiography">
    <author>
    <first-name>Joe</first-name>
    <last-name>Bob</last-name>
    <award>Trenton Literary</award>
    </author>
    <price>12</price>
    </book>
    <book style="textbook">
    <author>
    <first-name>Mary</first-name>
    <last-name>Bob</last-name>
    </author>
    <price>55</price>
    </book>
    <book style="novel" id="myfave">
    <author>
    <first-name>Toni</first-name>
    <last-name>Bob</last-name>
    </author>
    </book>
    </bookstore>

    Code Behind file:

    using (XmlReader reader = XmlReader.Create("book.xml"))
    {
    reader.MoveToContent();
    reader.Read();
    reader.Skip();
    reader.Skip();
    reader.ReadStartElement("book");
    reader.ReadStartElement("author");
    reader.ReadStartElement("first-name");
    xmlLabel.Text = reader.ReadString();
    reader.ReadEndElement();
    reader.ReadStartElement("last-name");
    xmlLabel.Text += " " + reader.ReadString();
    }

    So, that brings "Mary Bob". I don't understand why the Skip() method, if
    called twice, does not move fwd to the last book node and displays "Toni
    Bob". I have tried to find sth in MSDN and on the web, but without success.
    Maybe it is that I misunderstand the Read() method, but am not sure.
    For my understanding, after reader.MoveToContent() the reader is placed at
    the bookstore node. Calling Read() reads the current node (bookstore) and
    advances the reader to the next node (book). Then we call the Skip() method,
    so that we are at book style="textbook". Finally the last reader.Skip()
    advances to the third and last book node. If that would be true the result
    should be "Toni Bob", so there must be an error somewhere.

    Many thanks for your help!
    Martin
     
    Martin Eckart, Mar 18, 2007
    #1
    1. Advertising

  2. Martin Eckart

    Nail Guest

    You should use settings of XmlReader, like this:

    XmlReaderSettings settings = new XmlReaderSettings();
    settings.IgnoreWhitespace = true;
    using (XmlReader reader = XmlReader.Create("book.xml", settings))
    {
    ....
    }

    --
    Nail
    [MCPD: WEB]
     
    Nail, Mar 19, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. News Reader
    Replies:
    0
    Views:
    1,313
    News Reader
    Apr 10, 2006
  2. Cori
    Replies:
    13
    Views:
    717
    Bill Vermillion
    Mar 23, 2007
  3. Guru

    Regarding Exam 70-528

    Guru, Jan 7, 2009, in forum: Microsoft Certification
    Replies:
    1
    Views:
    555
    Martin Potestas
    Jan 8, 2009
  4. JFM
    Replies:
    2
    Views:
    390
    Michael D. Alligood
    Nov 16, 2006
  5. Pramodh
    Replies:
    2
    Views:
    459
Loading...

Share This Page