473,396 Members | 2,093 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

problems with XmlReader and HasAttributes

Hello everybody,

I saw the following code from MS and really have no idea, why the
attributes have to be parsed twice.
Can I omit the second loop?
If I lose some data, please explain it to me with a short XML data
example.

Thanks,
Michael Heitland

XmlTextReader rdr = new XmlTextReader ("data.xml");
while (rdr.Read ()) {
switch (rdr.NodeType) {
case XmlNodeType.Element:
Console.Write ("<" + rdr.Name);
while (rdr.MoveToNextAttribute())
Console.WriteLine (" {0}='{1}'",
rdr.Name, rdr.Value);
Console.Write (">");

//>>> WHY IS THIS SECOND LOOP NECESSARY?
if (rdr.HasAttributes) {
while (rdr.MoveToNextAttribute())
Console.WriteLine (" {0} ", rdr.Value);
}
//<<< WHY IS THIS SECOND LOOP NECESSARY?

break;
case XmlNodeType.Text:
Console.WriteLine (rdr.Value);
break;
case XmlNodeType.EndElement:
Console.WriteLine ("</{0}>", rdr.Name);
break;
}
}
Nov 16 '05 #1
1 1825
Dan
Michael,

To add to the confusion, given an XML snippet containing the following say:

<FIELD customer="Fred" id="12345" age="54" employed="permanent" />

now go trace through the XmlNotType.Element section of your switch to reveal
the following output:

<customer customer='Fred'
id='12345'
age='54'
employed='permanent'

So the contents of the second loop will never get processed, and the output
looks like it was formatted incorrectly.

Where was this code?

Thanks.

Dan.

"Michael Heitland" <mj*********@onlinehome.de> wrote in message
news:71**************************@posting.google.c om... Hello everybody,

I saw the following code from MS and really have no idea, why the
attributes have to be parsed twice.
Can I omit the second loop?
If I lose some data, please explain it to me with a short XML data
example.

Thanks,
Michael Heitland

XmlTextReader rdr = new XmlTextReader ("data.xml");
while (rdr.Read ()) {
switch (rdr.NodeType) {
case XmlNodeType.Element:
Console.Write ("<" + rdr.Name);
while (rdr.MoveToNextAttribute())
Console.WriteLine (" {0}='{1}'",
rdr.Name, rdr.Value);
Console.Write (">");

//>>> WHY IS THIS SECOND LOOP NECESSARY?
if (rdr.HasAttributes) {
while (rdr.MoveToNextAttribute())
Console.WriteLine (" {0} ", rdr.Value);
}
//<<< WHY IS THIS SECOND LOOP NECESSARY?

break;
case XmlNodeType.Text:
Console.WriteLine (rdr.Value);
break;
case XmlNodeType.EndElement:
Console.WriteLine ("</{0}>", rdr.Name);
break;
}
}

Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: xmlguy | last post by:
I believe this should be pretty elementary, but for some reason I cannot seem to think of how to write the an XML file from an incoming XML file. Basically this is what I do: Input: ...
2
by: xmlguy | last post by:
Cant seem to solve this problem I need to be able to re-use XmlReader and XPathDocument for an XSLT Transform. Basically I have defined following interfaces: Class Render (Common and...
6
by: Michael | last post by:
Hi All, I need a few suggestions. I have the following XML segment: <LookUp> <ControlType>CheckBoxGroup</ControlType> <DBField>LastMedDate</DBField> <ControlName>cmbGoal1</ControlName> <Values...
2
by: John Hite | last post by:
I'm writing an ASP.NET Web application in C# that has the following directory structure: MyApp\bin\MyApp.dll MyApp\conf\config.xml MyApp\webapp.aspx MyApp\webapp.aspx.cs ok. The MyApp.dll...
1
by: Angus Lepper | last post by:
I'm writing a stock ticker for a stock market simulation, and can load the data into the xmlreader in the first place, but can't figure out how to refresh/update the data in it. Any ideas? Code:...
0
by: Jen | last post by:
My main question: "How can I get a TextReader or Stream object from an existing XmlReader?". Read on for more: I have an existing XmlReader. Let's call it reader1. I'm creating another reader,...
6
by: J055 | last post by:
Hi I have the following code. I upload an XML file using the FileUpload object, store the stream in a session so the user gets the chance to confirm some options then pass the stream from the...
5
by: heday60 | last post by:
I've got this application that watches a directory for XML's and then parses them into a DB. It then moves them based on if the XML was succesful or a bad XML... foreach(FileInfo file in...
1
by: sarad1978 | last post by:
I'm reading in an xml document and everythings fine except there is an element <CreditCard expiration="02/2010" type="Visa">4111111111111111111</CreditCard> which clearly has attributes, but...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.