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. Advertisements

  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, Mar 19, 2007
    #2
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.