reading xml file to listbox
-
Hi, I've got a problem with reading in an xml file into a listbox This is the code: XmlDocument xmlDoc = new XmlDocument(); XmlTextReader xtr = new XmlTextReader(path); xtr.WhitespaceHandling = WhitespaceHandling.None; xmlDoc.Load(path); while (xtr.Read()) { if (xtr.Name.Equals("test") && xtr.NodeType == XmlNodeType.Element) { this.listBox.Items.Add( xtr.ReadElementString("test").ToString()); } Thread.Sleep(10); } This is the xml files content: 1111111 2222222 1111111 3333333 1111111 The problem is, it skips the "222222" and "333333" by not adding them into the listbox. The rest of the xml file does get loaded in the listbox. So this is what i get: 1111111 1111111 1111111 In stead of: 1111111 2222222 1111111 3333333 1111111 Can somebody point to me what i am doing wrong? Thanks in advance!
-
Hi, I've got a problem with reading in an xml file into a listbox This is the code: XmlDocument xmlDoc = new XmlDocument(); XmlTextReader xtr = new XmlTextReader(path); xtr.WhitespaceHandling = WhitespaceHandling.None; xmlDoc.Load(path); while (xtr.Read()) { if (xtr.Name.Equals("test") && xtr.NodeType == XmlNodeType.Element) { this.listBox.Items.Add( xtr.ReadElementString("test").ToString()); } Thread.Sleep(10); } This is the xml files content: 1111111 2222222 1111111 3333333 1111111 The problem is, it skips the "222222" and "333333" by not adding them into the listbox. The rest of the xml file does get loaded in the listbox. So this is what i get: 1111111 1111111 1111111 In stead of: 1111111 2222222 1111111 3333333 1111111 Can somebody point to me what i am doing wrong? Thanks in advance!
If you read the documentation for ReadElementString[^] you will see that it also moves the position in the XML file on. So for each iteration you are moving the position within the file twice.
Yustme wrote:
Thread.Sleep(10);
What is this for? It serves no purpose other than to slow down your application. Is that what you really want?
Upcoming Scottish Developers events: * Glasgow: Tell us what you want to see in 2007 My: Website | Blog | Photos
-
Hi, I've got a problem with reading in an xml file into a listbox This is the code: XmlDocument xmlDoc = new XmlDocument(); XmlTextReader xtr = new XmlTextReader(path); xtr.WhitespaceHandling = WhitespaceHandling.None; xmlDoc.Load(path); while (xtr.Read()) { if (xtr.Name.Equals("test") && xtr.NodeType == XmlNodeType.Element) { this.listBox.Items.Add( xtr.ReadElementString("test").ToString()); } Thread.Sleep(10); } This is the xml files content: 1111111 2222222 1111111 3333333 1111111 The problem is, it skips the "222222" and "333333" by not adding them into the listbox. The rest of the xml file does get loaded in the listbox. So this is what i get: 1111111 1111111 1111111 In stead of: 1111111 2222222 1111111 3333333 1111111 Can somebody point to me what i am doing wrong? Thanks in advance!
According to the documentation the
ReadElementString
method positions the reader on the node following the EndElement node; in you case on the next "test" StartElement node. Afterwards you callRead
inside the while statement which advances the reader to the next node and thereby your code only recognizes every second "test" element. I'm not that used to the XmlTextReader, so you have to experiment a bit which method gives you the desired result. By the way callingToString
on the result ofReadElementString
is redundant as it already returns a string.
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook
-
If you read the documentation for ReadElementString[^] you will see that it also moves the position in the XML file on. So for each iteration you are moving the position within the file twice.
Yustme wrote:
Thread.Sleep(10);
What is this for? It serves no purpose other than to slow down your application. Is that what you really want?
Upcoming Scottish Developers events: * Glasgow: Tell us what you want to see in 2007 My: Website | Blog | Photos
-
According to the documentation the
ReadElementString
method positions the reader on the node following the EndElement node; in you case on the next "test" StartElement node. Afterwards you callRead
inside the while statement which advances the reader to the next node and thereby your code only recognizes every second "test" element. I'm not that used to the XmlTextReader, so you have to experiment a bit which method gives you the desired result. By the way callingToString
on the result ofReadElementString
is redundant as it already returns a string.
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook
-
As said I'm not used to the
XmlTextReader
but after a quick glance at the documentation I would say that theReadString
method sounds quite promissing.
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook
-
As said I'm not used to the
XmlTextReader
but after a quick glance at the documentation I would say that theReadString
method sounds quite promissing.
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook
-
Hi, The sleep thread is to avoid that my pc is going to hang. There are over 2000 rows to read into the list box. Thanks for pointing that out to me. I didn't know it moves 2 positions within the file.
Yustme wrote:
The sleep thread is to avoid that my pc is going to hang. There are over 2000 rows to read into the list box.
That makes no sense what so ever. If you put the thread to sleep for 10 milliseconds 2000 times you are waiting 200 seconds (just over 3 minutes) to load in all the data.
Upcoming Scottish Developers events: * Glasgow: Tell us what you want to see in 2007 My: Website | Blog | Photos