70-320 xml question

Discussion in 'MCSD' started by =?Utf-8?B?Y2Jt?=, Jan 9, 2006.

  1. Hello

    Can someone explain to me the code in page 317 of book 70-320 of the MSFT
    self-paced training kit (Chapter 6, Lesson 2)?

    I reproduce it here (it shows how to read data using the XmlTextReader). I
    do not understand the if (reader.HasAttributes) block.

    XmlTextReader reader =new XmlTextReader(@"E:\emp.xml");
    while (reader.Read())
    {
    switch (reader.NodeType)
    {
    case XmlNodeType.Element:
    Console.Write("<" + reader.Name);
    while (reader.MoveToNextAttribute())
    {
    Console.Write(" " + reader.Name + "='" +
    reader.Value + "'");
    }
    Console.Write(">");
    if (reader.HasAttributes )
    {
    while (reader.MoveToNextAttribute())
    Console.Write(" " + reader.Value + " ");
    }
    break;
    case XmlNodeType.Text:
    Console.Write(reader.Value);
    break;
    case XmlNodeType.EndElement:
    Console.WriteLine(("</" + reader.Name + ">"));
    break;
    }
    }
    =?Utf-8?B?Y2Jt?=, Jan 9, 2006
    #1
    1. Advertising

  2. > Can someone explain to me the code in page 317 of book 70-320 of the MSFT
    > self-paced training kit (Chapter 6, Lesson 2)?
    >
    > I reproduce it here (it shows how to read data using the XmlTextReader). I
    > do not understand the if (reader.HasAttributes) block.


    XmlReader is a forward only reader for XML document and unfortunately it
    doesn't treat attributes as a node.

    For example in an XML document:

    <test>
    <book name="Foo">
    </book>
    </test>

    The reader.HasAttributes when you're in the <book> element is true. And thus
    you want to display the name="Foo" attribute.

    HTH,
    --
    Victor Hadianto
    Blog: http://www.hadianto.net/destination

    "cbm" <> wrote in message
    news:...
    > Hello
    >
    >
    > XmlTextReader reader =new XmlTextReader(@"E:\emp.xml");
    > while (reader.Read())
    > {
    > switch (reader.NodeType)
    > {
    > case XmlNodeType.Element:
    > Console.Write("<" + reader.Name);
    > while (reader.MoveToNextAttribute())
    > {
    > Console.Write(" " + reader.Name + "='" +
    > reader.Value + "'");
    > }
    > Console.Write(">");
    > if (reader.HasAttributes )
    > {
    > while (reader.MoveToNextAttribute())
    > Console.Write(" " + reader.Value + " ");
    > }
    > break;
    > case XmlNodeType.Text:
    > Console.Write(reader.Value);
    > break;
    > case XmlNodeType.EndElement:
    > Console.WriteLine(("</" + reader.Name + ">"));
    > break;
    > }
    > }
    >
    Victor Hadianto, Jan 9, 2006
    #2
    1. Advertising

  3. Maybe I didn't explain my problem clearly.

    What I don't understand is why do we need two blocks in the Element switch:
    first the while(reader.MoveToNextAttribute()) and then the if
    (reader.HasAttributes). It seems to me that if the element had attributes,
    they will be printed in the first while loop and the while loop inside if
    (reader.HasAttributes) will never be executed.

    If I am mistaken, can someone give me an example where we do go into the
    second while loop?

    thanks



    "Victor Hadianto" wrote:

    > > Can someone explain to me the code in page 317 of book 70-320 of the MSFT
    > > self-paced training kit (Chapter 6, Lesson 2)?
    > >
    > > I reproduce it here (it shows how to read data using the XmlTextReader). I
    > > do not understand the if (reader.HasAttributes) block.

    >
    > XmlReader is a forward only reader for XML document and unfortunately it
    > doesn't treat attributes as a node.
    >
    > For example in an XML document:
    >
    > <test>
    > <book name="Foo">
    > </book>
    > </test>
    >
    > The reader.HasAttributes when you're in the <book> element is true. And thus
    > you want to display the name="Foo" attribute.
    >
    > HTH,
    > --
    > Victor Hadianto
    > Blog: http://www.hadianto.net/destination
    >
    > "cbm" <> wrote in message
    > news:...
    > > Hello
    > >
    > >
    > > XmlTextReader reader =new XmlTextReader(@"E:\emp.xml");
    > > while (reader.Read())
    > > {
    > > switch (reader.NodeType)
    > > {
    > > case XmlNodeType.Element:
    > > Console.Write("<" + reader.Name);
    > > while (reader.MoveToNextAttribute())
    > > {
    > > Console.Write(" " + reader.Name + "='" +
    > > reader.Value + "'");
    > > }
    > > Console.Write(">");
    > > if (reader.HasAttributes )
    > > {
    > > while (reader.MoveToNextAttribute())
    > > Console.Write(" " + reader.Value + " ");
    > > }
    > > break;
    > > case XmlNodeType.Text:
    > > Console.Write(reader.Value);
    > > break;
    > > case XmlNodeType.EndElement:
    > > Console.WriteLine(("</" + reader.Name + ">"));
    > > break;
    > > }
    > > }
    > >

    >
    >
    >
    =?Utf-8?B?Y2Jt?=, Jan 10, 2006
    #3
  4. > What I don't understand is why do we need two blocks in the Element
    switch:
    > first the while(reader.MoveToNextAttribute()) and then the if
    > (reader.HasAttributes). It seems to me that if the element had attributes,
    > they will be printed in the first while loop and the while loop inside if
    > (reader.HasAttributes) will never be executed.


    The code looks correct to me.

    * If there's an attribute in this element (ie: the book element in my
    example) then
    * Print all the attributes (using the while (reader.MoveToNextAttribute
    method)

    If there's no attribute then the while loop will never be executed. If you
    have more than 1 attribute then the while loop will print all the
    attributes.

    --
    Victor Hadianto
    Blog: http://www.hadianto.net/destination

    > If I am mistaken, can someone give me an example where we do go into the
    > second while loop?
    >
    > thanks
    >
    >
    >
    > "Victor Hadianto" wrote:
    >
    > > > Can someone explain to me the code in page 317 of book 70-320 of the

    MSFT
    > > > self-paced training kit (Chapter 6, Lesson 2)?
    > > >
    > > > I reproduce it here (it shows how to read data using the

    XmlTextReader). I
    > > > do not understand the if (reader.HasAttributes) block.

    > >
    > > XmlReader is a forward only reader for XML document and unfortunately it
    > > doesn't treat attributes as a node.
    > >
    > > For example in an XML document:
    > >
    > > <test>
    > > <book name="Foo">
    > > </book>
    > > </test>
    > >
    > > The reader.HasAttributes when you're in the <book> element is true. And

    thus
    > > you want to display the name="Foo" attribute.
    > >
    > > HTH,
    > > --
    > > Victor Hadianto
    > > Blog: http://www.hadianto.net/destination
    > >
    > > "cbm" <> wrote in message
    > > news:...
    > > > Hello
    > > >
    > > >
    > > > XmlTextReader reader =new XmlTextReader(@"E:\emp.xml");
    > > > while (reader.Read())
    > > > {
    > > > switch (reader.NodeType)
    > > > {
    > > > case XmlNodeType.Element:
    > > > Console.Write("<" + reader.Name);
    > > > while (reader.MoveToNextAttribute())
    > > > {
    > > > Console.Write(" " + reader.Name + "='" +
    > > > reader.Value + "'");
    > > > }
    > > > Console.Write(">");
    > > > if (reader.HasAttributes )
    > > > {
    > > > while (reader.MoveToNextAttribute())
    > > > Console.Write(" " + reader.Value + " ");
    > > > }
    > > > break;
    > > > case XmlNodeType.Text:
    > > > Console.Write(reader.Value);
    > > > break;
    > > > case XmlNodeType.EndElement:
    > > > Console.WriteLine(("</" + reader.Name + ">"));
    > > > break;
    > > > }
    > > > }
    > > >

    > >
    > >
    > >
    Victor Hadianto, Jan 11, 2006
    #4
  5. Ok. I'll try one last time.

    According to me, this part is not needed:

    if (reader.HasAttributes )
    {
    while (reader.MoveToNextAttribute())
    Console.Write(" " + reader.Value + " ");
    }

    Let's use your example from earlier (with one extra attribute):

    <test>
    <book name="Foo" value="">
    </book>
    </test>

    Given that the book element has two attributes, we will go into the first
    while loop twice. So when this part executes

    if (reader.HasAttributes )
    {
    while (reader.MoveToNextAttribute())
    Console.Write(" " + reader.Value + " ");
    }

    This has been printed in the console:

    <test>
    <book name="Foo" value="">

    Then, the test if(reader.HasAttributes) will return true BUT the
    while(reader.MoveToNextAttribute()) will return false because we already
    iterated through all the attributes in the first while loop.

    So, why do we need this second while loop???? Can anyone find an example
    where it is needed?

    Thanks for your help anyway ...

    "Victor Hadianto" wrote:

    > > What I don't understand is why do we need two blocks in the Element

    > switch:
    > > first the while(reader.MoveToNextAttribute()) and then the if
    > > (reader.HasAttributes). It seems to me that if the element had attributes,
    > > they will be printed in the first while loop and the while loop inside if
    > > (reader.HasAttributes) will never be executed.

    >
    > The code looks correct to me.
    >
    > * If there's an attribute in this element (ie: the book element in my
    > example) then
    > * Print all the attributes (using the while (reader.MoveToNextAttribute
    > method)
    >
    > If there's no attribute then the while loop will never be executed. If you
    > have more than 1 attribute then the while loop will print all the
    > attributes.
    >
    > --
    > Victor Hadianto
    > Blog: http://www.hadianto.net/destination
    >
    > > If I am mistaken, can someone give me an example where we do go into the
    > > second while loop?
    > >
    > > thanks
    > >
    > >
    > >
    > > "Victor Hadianto" wrote:
    > >
    > > > > Can someone explain to me the code in page 317 of book 70-320 of the

    > MSFT
    > > > > self-paced training kit (Chapter 6, Lesson 2)?
    > > > >
    > > > > I reproduce it here (it shows how to read data using the

    > XmlTextReader). I
    > > > > do not understand the if (reader.HasAttributes) block.
    > > >
    > > > XmlReader is a forward only reader for XML document and unfortunately it
    > > > doesn't treat attributes as a node.
    > > >
    > > > For example in an XML document:
    > > >
    > > > <test>
    > > > <book name="Foo">
    > > > </book>
    > > > </test>
    > > >
    > > > The reader.HasAttributes when you're in the <book> element is true. And

    > thus
    > > > you want to display the name="Foo" attribute.
    > > >
    > > > HTH,
    > > > --
    > > > Victor Hadianto
    > > > Blog: http://www.hadianto.net/destination
    > > >
    > > > "cbm" <> wrote in message
    > > > news:...
    > > > > Hello
    > > > >
    > > > >
    > > > > XmlTextReader reader =new XmlTextReader(@"E:\emp.xml");
    > > > > while (reader.Read())
    > > > > {
    > > > > switch (reader.NodeType)
    > > > > {
    > > > > case XmlNodeType.Element:
    > > > > Console.Write("<" + reader.Name);
    > > > > while (reader.MoveToNextAttribute())
    > > > > {
    > > > > Console.Write(" " + reader.Name + "='" +
    > > > > reader.Value + "'");
    > > > > }
    > > > > Console.Write(">");
    > > > > if (reader.HasAttributes )
    > > > > {
    > > > > while (reader.MoveToNextAttribute())
    > > > > Console.Write(" " + reader.Value + " ");
    > > > > }
    > > > > break;
    > > > > case XmlNodeType.Text:
    > > > > Console.Write(reader.Value);
    > > > > break;
    > > > > case XmlNodeType.EndElement:
    > > > > Console.WriteLine(("</" + reader.Name + ">"));
    > > > > break;
    > > > > }
    > > > > }
    > > > >
    > > >
    > > >
    > > >

    >
    >
    >
    =?Utf-8?B?Y2Jt?=, Jan 11, 2006
    #5
  6. January 11, 2006

    The While loop only executes ONCE, because IF Statements are NOT loops....
    when the code exits the While loop and hits the "}" part of the If
    statement, it does NOT run all the code again.

    Hope this helps...

    --

    Joseph Bittman
    Microsoft Certified Solution Developer
    Microsoft Most Valuable Professional -- DPM

    Blog/Web Site: http://71.39.42.23/



    "cbm" <> wrote in message
    news:...
    > Ok. I'll try one last time.
    >
    > According to me, this part is not needed:
    >
    > if (reader.HasAttributes )
    > {
    > while (reader.MoveToNextAttribute())
    > Console.Write(" " + reader.Value + " ");
    > }
    >
    > Let's use your example from earlier (with one extra attribute):
    >
    > <test>
    > <book name="Foo" value="">
    > </book>
    > </test>
    >
    > Given that the book element has two attributes, we will go into the first
    > while loop twice. So when this part executes
    >
    > if (reader.HasAttributes )
    > {
    > while (reader.MoveToNextAttribute())
    > Console.Write(" " + reader.Value + " ");
    > }
    >
    > This has been printed in the console:
    >
    > <test>
    > <book name="Foo" value="">
    >
    > Then, the test if(reader.HasAttributes) will return true BUT the
    > while(reader.MoveToNextAttribute()) will return false because we already
    > iterated through all the attributes in the first while loop.
    >
    > So, why do we need this second while loop???? Can anyone find an example
    > where it is needed?
    >
    > Thanks for your help anyway ...
    >
    > "Victor Hadianto" wrote:
    >
    >> > What I don't understand is why do we need two blocks in the Element

    >> switch:
    >> > first the while(reader.MoveToNextAttribute()) and then the if
    >> > (reader.HasAttributes). It seems to me that if the element had
    >> > attributes,
    >> > they will be printed in the first while loop and the while loop inside
    >> > if
    >> > (reader.HasAttributes) will never be executed.

    >>
    >> The code looks correct to me.
    >>
    >> * If there's an attribute in this element (ie: the book element in my
    >> example) then
    >> * Print all the attributes (using the while
    >> (reader.MoveToNextAttribute
    >> method)
    >>
    >> If there's no attribute then the while loop will never be executed. If
    >> you
    >> have more than 1 attribute then the while loop will print all the
    >> attributes.
    >>
    >> --
    >> Victor Hadianto
    >> Blog: http://www.hadianto.net/destination
    >>
    >> > If I am mistaken, can someone give me an example where we do go into
    >> > the
    >> > second while loop?
    >> >
    >> > thanks
    >> >
    >> >
    >> >
    >> > "Victor Hadianto" wrote:
    >> >
    >> > > > Can someone explain to me the code in page 317 of book 70-320 of
    >> > > > the

    >> MSFT
    >> > > > self-paced training kit (Chapter 6, Lesson 2)?
    >> > > >
    >> > > > I reproduce it here (it shows how to read data using the

    >> XmlTextReader). I
    >> > > > do not understand the if (reader.HasAttributes) block.
    >> > >
    >> > > XmlReader is a forward only reader for XML document and unfortunately
    >> > > it
    >> > > doesn't treat attributes as a node.
    >> > >
    >> > > For example in an XML document:
    >> > >
    >> > > <test>
    >> > > <book name="Foo">
    >> > > </book>
    >> > > </test>
    >> > >
    >> > > The reader.HasAttributes when you're in the <book> element is true.
    >> > > And

    >> thus
    >> > > you want to display the name="Foo" attribute.
    >> > >
    >> > > HTH,
    >> > > --
    >> > > Victor Hadianto
    >> > > Blog: http://www.hadianto.net/destination
    >> > >
    >> > > "cbm" <> wrote in message
    >> > > news:...
    >> > > > Hello
    >> > > >
    >> > > >
    >> > > > XmlTextReader reader =new XmlTextReader(@"E:\emp.xml");
    >> > > > while (reader.Read())
    >> > > > {
    >> > > > switch (reader.NodeType)
    >> > > > {
    >> > > > case XmlNodeType.Element:
    >> > > > Console.Write("<" + reader.Name);
    >> > > > while (reader.MoveToNextAttribute())
    >> > > > {
    >> > > > Console.Write(" " + reader.Name + "='" +
    >> > > > reader.Value + "'");
    >> > > > }
    >> > > > Console.Write(">");
    >> > > > if (reader.HasAttributes )
    >> > > > {
    >> > > > while (reader.MoveToNextAttribute())
    >> > > > Console.Write(" " + reader.Value + " ");
    >> > > > }
    >> > > > break;
    >> > > > case XmlNodeType.Text:
    >> > > > Console.Write(reader.Value);
    >> > > > break;
    >> > > > case XmlNodeType.EndElement:
    >> > > > Console.WriteLine(("</" + reader.Name + ">"));
    >> > > > break;
    >> > > > }
    >> > > > }
    >> > > >
    >> > >
    >> > >
    >> > >

    >>
    >>
    >>
    Joseph Bittman MVP MCSD, Jan 11, 2006
    #6
  7. Joseph is correct. I think you misunderstood the difference between the IF
    and the WHILE construct.
    Please review:

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrftheifstatement.asp

    and

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrftheifstatement.asp

    HTH,
    --
    Victor Hadianto
    Blog: http://www.hadianto.net/destination

    "Joseph Bittman MVP MCSD" <> wrote in message
    news:%...
    > January 11, 2006
    >
    > The While loop only executes ONCE, because IF Statements are NOT

    loops....
    > when the code exits the While loop and hits the "}" part of the If
    > statement, it does NOT run all the code again.
    >
    > Hope this helps...
    >
    > --
    >
    > Joseph Bittman
    > Microsoft Certified Solution Developer
    > Microsoft Most Valuable Professional -- DPM
    >
    > Blog/Web Site: http://71.39.42.23/
    >
    >
    >
    > "cbm" <> wrote in message
    > news:...
    > > Ok. I'll try one last time.
    > >
    > > According to me, this part is not needed:
    > >
    > > if (reader.HasAttributes )
    > > {
    > > while (reader.MoveToNextAttribute())
    > > Console.Write(" " + reader.Value + " ");
    > > }
    > >
    > > Let's use your example from earlier (with one extra attribute):
    > >
    > > <test>
    > > <book name="Foo" value="">
    > > </book>
    > > </test>
    > >
    > > Given that the book element has two attributes, we will go into the

    first
    > > while loop twice. So when this part executes
    > >
    > > if (reader.HasAttributes )
    > > {
    > > while (reader.MoveToNextAttribute())
    > > Console.Write(" " + reader.Value + " ");
    > > }
    > >
    > > This has been printed in the console:
    > >
    > > <test>
    > > <book name="Foo" value="">
    > >
    > > Then, the test if(reader.HasAttributes) will return true BUT the
    > > while(reader.MoveToNextAttribute()) will return false because we already
    > > iterated through all the attributes in the first while loop.
    > >
    > > So, why do we need this second while loop???? Can anyone find an example
    > > where it is needed?
    > >
    > > Thanks for your help anyway ...
    > >
    > > "Victor Hadianto" wrote:
    > >
    > >> > What I don't understand is why do we need two blocks in the Element
    > >> switch:
    > >> > first the while(reader.MoveToNextAttribute()) and then the if
    > >> > (reader.HasAttributes). It seems to me that if the element had
    > >> > attributes,
    > >> > they will be printed in the first while loop and the while loop

    inside
    > >> > if
    > >> > (reader.HasAttributes) will never be executed.
    > >>
    > >> The code looks correct to me.
    > >>
    > >> * If there's an attribute in this element (ie: the book element in my
    > >> example) then
    > >> * Print all the attributes (using the while
    > >> (reader.MoveToNextAttribute
    > >> method)
    > >>
    > >> If there's no attribute then the while loop will never be executed. If
    > >> you
    > >> have more than 1 attribute then the while loop will print all the
    > >> attributes.
    > >>
    > >> --
    > >> Victor Hadianto
    > >> Blog: http://www.hadianto.net/destination
    > >>
    > >> > If I am mistaken, can someone give me an example where we do go into
    > >> > the
    > >> > second while loop?
    > >> >
    > >> > thanks
    > >> >
    > >> >
    > >> >
    > >> > "Victor Hadianto" wrote:
    > >> >
    > >> > > > Can someone explain to me the code in page 317 of book 70-320 of
    > >> > > > the
    > >> MSFT
    > >> > > > self-paced training kit (Chapter 6, Lesson 2)?
    > >> > > >
    > >> > > > I reproduce it here (it shows how to read data using the
    > >> XmlTextReader). I
    > >> > > > do not understand the if (reader.HasAttributes) block.
    > >> > >
    > >> > > XmlReader is a forward only reader for XML document and

    unfortunately
    > >> > > it
    > >> > > doesn't treat attributes as a node.
    > >> > >
    > >> > > For example in an XML document:
    > >> > >
    > >> > > <test>
    > >> > > <book name="Foo">
    > >> > > </book>
    > >> > > </test>
    > >> > >
    > >> > > The reader.HasAttributes when you're in the <book> element is true.
    > >> > > And
    > >> thus
    > >> > > you want to display the name="Foo" attribute.
    > >> > >
    > >> > > HTH,
    > >> > > --
    > >> > > Victor Hadianto
    > >> > > Blog: http://www.hadianto.net/destination
    > >> > >
    > >> > > "cbm" <> wrote in message
    > >> > > news:...
    > >> > > > Hello
    > >> > > >
    > >> > > >
    > >> > > > XmlTextReader reader =new XmlTextReader(@"E:\emp.xml");
    > >> > > > while (reader.Read())
    > >> > > > {
    > >> > > > switch (reader.NodeType)
    > >> > > > {
    > >> > > > case XmlNodeType.Element:
    > >> > > > Console.Write("<" + reader.Name);
    > >> > > > while (reader.MoveToNextAttribute())
    > >> > > > {
    > >> > > > Console.Write(" " + reader.Name + "='" +
    > >> > > > reader.Value + "'");
    > >> > > > }
    > >> > > > Console.Write(">");
    > >> > > > if (reader.HasAttributes )
    > >> > > > {
    > >> > > > while (reader.MoveToNextAttribute())
    > >> > > > Console.Write(" " + reader.Value + " ");
    > >> > > > }
    > >> > > > break;
    > >> > > > case XmlNodeType.Text:
    > >> > > > Console.Write(reader.Value);
    > >> > > > break;
    > >> > > > case XmlNodeType.EndElement:
    > >> > > > Console.WriteLine(("</" + reader.Name + ">"));
    > >> > > > break;
    > >> > > > }
    > >> > > > }
    > >> > > >
    > >> > >
    > >> > >
    > >> > >
    > >>
    > >>
    > >>

    >
    >
    Victor Hadianto, Jan 11, 2006
    #7
  8. Wow. Is anyone else watching this train wreck happen? Victor and Joseph
    are drowning cbm with gallons of mindless drivel spewing forth from their
    keyboards. I wasn't going to chime in on this thread because I thought it
    was such a dumb question to begin with. If you have a question about what a
    particular piece of code does, put it in the debugger and step through it.
    Then you will see what happens for sure.

    Anyway, I've gotten a good laugh watching Victor and Joseph NOT answering
    his question.

    To answer your question cbm (assuming you haven't drowned to death in the
    mindless drivel), you are correct. The if statement containing the second
    while loop is NOT NEEDED. It does no harm and it does no good. Obviously
    the author did not read his code very well.


    Death from Above


    "Victor Hadianto" <> wrote in message
    news:...
    > Joseph is correct. I think you misunderstood the difference between the IF
    > and the WHILE construct.
    > Please review:
    >
    > http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrftheifstatement.asp
    >
    > and
    >
    > http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrftheifstatement.asp
    >
    > HTH,
    > --
    > Victor Hadianto
    > Blog: http://www.hadianto.net/destination
    >
    > "Joseph Bittman MVP MCSD" <> wrote in message
    > news:%...
    >> January 11, 2006
    >>
    >> The While loop only executes ONCE, because IF Statements are NOT

    > loops....
    >> when the code exits the While loop and hits the "}" part of the If
    >> statement, it does NOT run all the code again.
    >>
    >> Hope this helps...
    >>
    >> --
    >>
    >> Joseph Bittman
    >> Microsoft Certified Solution Developer
    >> Microsoft Most Valuable Professional -- DPM
    >>
    >> Blog/Web Site: http://71.39.42.23/
    >>
    >>
    >>
    >> "cbm" <> wrote in message
    >> news:...
    >> > Ok. I'll try one last time.
    >> >
    >> > According to me, this part is not needed:
    >> >
    >> > if (reader.HasAttributes )
    >> > {
    >> > while (reader.MoveToNextAttribute())
    >> > Console.Write(" " + reader.Value + " ");
    >> > }
    >> >
    >> > Let's use your example from earlier (with one extra attribute):
    >> >
    >> > <test>
    >> > <book name="Foo" value="">
    >> > </book>
    >> > </test>
    >> >
    >> > Given that the book element has two attributes, we will go into the

    > first
    >> > while loop twice. So when this part executes
    >> >
    >> > if (reader.HasAttributes )
    >> > {
    >> > while (reader.MoveToNextAttribute())
    >> > Console.Write(" " + reader.Value + " ");
    >> > }
    >> >
    >> > This has been printed in the console:
    >> >
    >> > <test>
    >> > <book name="Foo" value="">
    >> >
    >> > Then, the test if(reader.HasAttributes) will return true BUT the
    >> > while(reader.MoveToNextAttribute()) will return false because we
    >> > already
    >> > iterated through all the attributes in the first while loop.
    >> >
    >> > So, why do we need this second while loop???? Can anyone find an
    >> > example
    >> > where it is needed?
    >> >
    >> > Thanks for your help anyway ...
    >> >
    >> > "Victor Hadianto" wrote:
    >> >
    >> >> > What I don't understand is why do we need two blocks in the Element
    >> >> switch:
    >> >> > first the while(reader.MoveToNextAttribute()) and then the if
    >> >> > (reader.HasAttributes). It seems to me that if the element had
    >> >> > attributes,
    >> >> > they will be printed in the first while loop and the while loop

    > inside
    >> >> > if
    >> >> > (reader.HasAttributes) will never be executed.
    >> >>
    >> >> The code looks correct to me.
    >> >>
    >> >> * If there's an attribute in this element (ie: the book element in my
    >> >> example) then
    >> >> * Print all the attributes (using the while
    >> >> (reader.MoveToNextAttribute
    >> >> method)
    >> >>
    >> >> If there's no attribute then the while loop will never be executed. If
    >> >> you
    >> >> have more than 1 attribute then the while loop will print all the
    >> >> attributes.
    >> >>
    >> >> --
    >> >> Victor Hadianto
    >> >> Blog: http://www.hadianto.net/destination
    >> >>
    >> >> > If I am mistaken, can someone give me an example where we do go into
    >> >> > the
    >> >> > second while loop?
    >> >> >
    >> >> > thanks
    >> >> >
    >> >> >
    >> >> >
    >> >> > "Victor Hadianto" wrote:
    >> >> >
    >> >> > > > Can someone explain to me the code in page 317 of book 70-320 of
    >> >> > > > the
    >> >> MSFT
    >> >> > > > self-paced training kit (Chapter 6, Lesson 2)?
    >> >> > > >
    >> >> > > > I reproduce it here (it shows how to read data using the
    >> >> XmlTextReader). I
    >> >> > > > do not understand the if (reader.HasAttributes) block.
    >> >> > >
    >> >> > > XmlReader is a forward only reader for XML document and

    > unfortunately
    >> >> > > it
    >> >> > > doesn't treat attributes as a node.
    >> >> > >
    >> >> > > For example in an XML document:
    >> >> > >
    >> >> > > <test>
    >> >> > > <book name="Foo">
    >> >> > > </book>
    >> >> > > </test>
    >> >> > >
    >> >> > > The reader.HasAttributes when you're in the <book> element is
    >> >> > > true.
    >> >> > > And
    >> >> thus
    >> >> > > you want to display the name="Foo" attribute.
    >> >> > >
    >> >> > > HTH,
    >> >> > > --
    >> >> > > Victor Hadianto
    >> >> > > Blog: http://www.hadianto.net/destination
    >> >> > >
    >> >> > > "cbm" <> wrote in message
    >> >> > > news:...
    >> >> > > > Hello
    >> >> > > >
    >> >> > > >
    >> >> > > > XmlTextReader reader =new XmlTextReader(@"E:\emp.xml");
    >> >> > > > while (reader.Read())
    >> >> > > > {
    >> >> > > > switch (reader.NodeType)
    >> >> > > > {
    >> >> > > > case XmlNodeType.Element:
    >> >> > > > Console.Write("<" + reader.Name);
    >> >> > > > while (reader.MoveToNextAttribute())
    >> >> > > > {
    >> >> > > > Console.Write(" " + reader.Name + "='" +
    >> >> > > > reader.Value + "'");
    >> >> > > > }
    >> >> > > > Console.Write(">");
    >> >> > > > if (reader.HasAttributes )
    >> >> > > > {
    >> >> > > > while (reader.MoveToNextAttribute())
    >> >> > > > Console.Write(" " + reader.Value + " ");
    >> >> > > > }
    >> >> > > > break;
    >> >> > > > case XmlNodeType.Text:
    >> >> > > > Console.Write(reader.Value);
    >> >> > > > break;
    >> >> > > > case XmlNodeType.EndElement:
    >> >> > > > Console.WriteLine(("</" + reader.Name + ">"));
    >> >> > > > break;
    >> >> > > > }
    >> >> > > > }
    >> >> > > >
    >> >> > >
    >> >> > >
    >> >> > >
    >> >>
    >> >>
    >> >>

    >>
    >>

    >
    >
    Death from Above, Jan 11, 2006
    #8
  9. Silly me, it was _me_ who didn't read the original code and cmb's question
    very well. I realised what the question is now. In that case yes then the
    second while loop is not needed.

    --
    Victor Hadianto
    Blog: http://www.hadianto.net/destination

    "Death from Above" <> wrote in message
    news:...
    >
    > Wow. Is anyone else watching this train wreck happen? Victor and Joseph
    > are drowning cbm with gallons of mindless drivel spewing forth from their
    > keyboards. I wasn't going to chime in on this thread because I thought it
    > was such a dumb question to begin with. If you have a question about what

    a
    > particular piece of code does, put it in the debugger and step through it.
    > Then you will see what happens for sure.
    >
    > Anyway, I've gotten a good laugh watching Victor and Joseph NOT answering
    > his question.
    >
    > To answer your question cbm (assuming you haven't drowned to death in the
    > mindless drivel), you are correct. The if statement containing the second
    > while loop is NOT NEEDED. It does no harm and it does no good. Obviously
    > the author did not read his code very well.
    >
    >
    > Death from Above
    >
    >
    > "Victor Hadianto" <> wrote in message
    > news:...
    > > Joseph is correct. I think you misunderstood the difference between the

    IF
    > > and the WHILE construct.
    > > Please review:
    > >
    > >

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrftheifstatement.asp
    > >
    > > and
    > >
    > >

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrftheifstatement.asp
    > >
    > > HTH,
    > > --
    > > Victor Hadianto
    > > Blog: http://www.hadianto.net/destination
    > >
    > > "Joseph Bittman MVP MCSD" <> wrote in message
    > > news:%...
    > >> January 11, 2006
    > >>
    > >> The While loop only executes ONCE, because IF Statements are NOT

    > > loops....
    > >> when the code exits the While loop and hits the "}" part of the If
    > >> statement, it does NOT run all the code again.
    > >>
    > >> Hope this helps...
    > >>
    > >> --
    > >>
    > >> Joseph Bittman
    > >> Microsoft Certified Solution Developer
    > >> Microsoft Most Valuable Professional -- DPM
    > >>
    > >> Blog/Web Site: http://71.39.42.23/
    > >>
    > >>
    > >>
    > >> "cbm" <> wrote in message
    > >> news:...
    > >> > Ok. I'll try one last time.
    > >> >
    > >> > According to me, this part is not needed:
    > >> >
    > >> > if (reader.HasAttributes )
    > >> > {
    > >> > while (reader.MoveToNextAttribute())
    > >> > Console.Write(" " + reader.Value + " ");
    > >> > }
    > >> >
    > >> > Let's use your example from earlier (with one extra attribute):
    > >> >
    > >> > <test>
    > >> > <book name="Foo" value="">
    > >> > </book>
    > >> > </test>
    > >> >
    > >> > Given that the book element has two attributes, we will go into the

    > > first
    > >> > while loop twice. So when this part executes
    > >> >
    > >> > if (reader.HasAttributes )
    > >> > {
    > >> > while (reader.MoveToNextAttribute())
    > >> > Console.Write(" " + reader.Value + " ");
    > >> > }
    > >> >
    > >> > This has been printed in the console:
    > >> >
    > >> > <test>
    > >> > <book name="Foo" value="">
    > >> >
    > >> > Then, the test if(reader.HasAttributes) will return true BUT the
    > >> > while(reader.MoveToNextAttribute()) will return false because we
    > >> > already
    > >> > iterated through all the attributes in the first while loop.
    > >> >
    > >> > So, why do we need this second while loop???? Can anyone find an
    > >> > example
    > >> > where it is needed?
    > >> >
    > >> > Thanks for your help anyway ...
    > >> >
    > >> > "Victor Hadianto" wrote:
    > >> >
    > >> >> > What I don't understand is why do we need two blocks in the

    Element
    > >> >> switch:
    > >> >> > first the while(reader.MoveToNextAttribute()) and then the if
    > >> >> > (reader.HasAttributes). It seems to me that if the element had
    > >> >> > attributes,
    > >> >> > they will be printed in the first while loop and the while loop

    > > inside
    > >> >> > if
    > >> >> > (reader.HasAttributes) will never be executed.
    > >> >>
    > >> >> The code looks correct to me.
    > >> >>
    > >> >> * If there's an attribute in this element (ie: the book element in

    my
    > >> >> example) then
    > >> >> * Print all the attributes (using the while
    > >> >> (reader.MoveToNextAttribute
    > >> >> method)
    > >> >>
    > >> >> If there's no attribute then the while loop will never be executed.

    If
    > >> >> you
    > >> >> have more than 1 attribute then the while loop will print all the
    > >> >> attributes.
    > >> >>
    > >> >> --
    > >> >> Victor Hadianto
    > >> >> Blog: http://www.hadianto.net/destination
    > >> >>
    > >> >> > If I am mistaken, can someone give me an example where we do go

    into
    > >> >> > the
    > >> >> > second while loop?
    > >> >> >
    > >> >> > thanks
    > >> >> >
    > >> >> >
    > >> >> >
    > >> >> > "Victor Hadianto" wrote:
    > >> >> >
    > >> >> > > > Can someone explain to me the code in page 317 of book 70-320

    of
    > >> >> > > > the
    > >> >> MSFT
    > >> >> > > > self-paced training kit (Chapter 6, Lesson 2)?
    > >> >> > > >
    > >> >> > > > I reproduce it here (it shows how to read data using the
    > >> >> XmlTextReader). I
    > >> >> > > > do not understand the if (reader.HasAttributes) block.
    > >> >> > >
    > >> >> > > XmlReader is a forward only reader for XML document and

    > > unfortunately
    > >> >> > > it
    > >> >> > > doesn't treat attributes as a node.
    > >> >> > >
    > >> >> > > For example in an XML document:
    > >> >> > >
    > >> >> > > <test>
    > >> >> > > <book name="Foo">
    > >> >> > > </book>
    > >> >> > > </test>
    > >> >> > >
    > >> >> > > The reader.HasAttributes when you're in the <book> element is
    > >> >> > > true.
    > >> >> > > And
    > >> >> thus
    > >> >> > > you want to display the name="Foo" attribute.
    > >> >> > >
    > >> >> > > HTH,
    > >> >> > > --
    > >> >> > > Victor Hadianto
    > >> >> > > Blog: http://www.hadianto.net/destination
    > >> >> > >
    > >> >> > > "cbm" <> wrote in message
    > >> >> > > news:...
    > >> >> > > > Hello
    > >> >> > > >
    > >> >> > > >
    > >> >> > > > XmlTextReader reader =new XmlTextReader(@"E:\emp.xml");
    > >> >> > > > while (reader.Read())
    > >> >> > > > {
    > >> >> > > > switch (reader.NodeType)
    > >> >> > > > {
    > >> >> > > > case XmlNodeType.Element:
    > >> >> > > > Console.Write("<" + reader.Name);
    > >> >> > > > while (reader.MoveToNextAttribute())
    > >> >> > > > {
    > >> >> > > > Console.Write(" " + reader.Name + "='" +
    > >> >> > > > reader.Value + "'");
    > >> >> > > > }
    > >> >> > > > Console.Write(">");
    > >> >> > > > if (reader.HasAttributes )
    > >> >> > > > {
    > >> >> > > > while (reader.MoveToNextAttribute())
    > >> >> > > > Console.Write(" " + reader.Value + "

    ");
    > >> >> > > > }
    > >> >> > > > break;
    > >> >> > > > case XmlNodeType.Text:
    > >> >> > > > Console.Write(reader.Value);
    > >> >> > > > break;
    > >> >> > > > case XmlNodeType.EndElement:
    > >> >> > > > Console.WriteLine(("</" + reader.Name + ">"));
    > >> >> > > > break;
    > >> >> > > > }
    > >> >> > > > }
    > >> >> > > >
    > >> >> > >
    > >> >> > >
    > >> >> > >
    > >> >>
    > >> >>
    > >> >>
    > >>
    > >>

    > >
    > >

    >
    >
    Victor Hadianto, Jan 12, 2006
    #9
  10. First, THANK YOU so much "Death from Above" (your name is scary ...). At
    last someone read my question properly :) BTW, I did put the code through the
    debugger which only confirmed what I thought but given that this is from the
    MSFT book, I wanted someone else to confirm it.

    Second, guys I'm preparing for MCSD.NET certification: I do know the
    difference between IF and WHILE.

    See you around
    =?Utf-8?B?Y2Jt?=, Jan 12, 2006
    #10
    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. saucer
    Replies:
    0
    Views:
    1,122
    saucer
    Jul 14, 2003
  2. Bernard
    Replies:
    0
    Views:
    1,039
    Bernard
    Jul 14, 2003
  3. Greg
    Replies:
    1
    Views:
    639
    The Poster Formerly Known as Kline Sphere
    Jul 17, 2004
  4. Pookee
    Replies:
    1
    Views:
    281
    Hermit Dave
    Sep 29, 2004
  5. BOGABOGA
    Replies:
    3
    Views:
    542
    BOGABOGA
    Apr 12, 2005
Loading...

Share This Page