Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. reading xml file to listbox

reading xml file to listbox

Scheduled Pinned Locked Moved C#
xmljsonhelpquestion
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    Yustme
    wrote on last edited by
    #1

    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!

    C S 2 Replies Last reply
    0
    • Y Yustme

      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!

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      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

      Y 1 Reply Last reply
      0
      • Y Yustme

        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!

        S Offline
        S Offline
        Stefan Troschuetz
        wrote on last edited by
        #3

        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 call Read 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 calling ToString on the result of ReadElementString 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

        www.troschuetz.de

        Y 1 Reply Last reply
        0
        • C Colin Angus Mackay

          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

          Y Offline
          Y Offline
          Yustme
          wrote on last edited by
          #4

          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.

          C 1 Reply Last reply
          0
          • S Stefan Troschuetz

            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 call Read 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 calling ToString on the result of ReadElementString 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

            www.troschuetz.de

            Y Offline
            Y Offline
            Yustme
            wrote on last edited by
            #5

            Hi, Can you tell me what method you used for these kind of operations? Thanks in advance!

            S 1 Reply Last reply
            0
            • Y Yustme

              Hi, Can you tell me what method you used for these kind of operations? Thanks in advance!

              S Offline
              S Offline
              Stefan Troschuetz
              wrote on last edited by
              #6

              As said I'm not used to the XmlTextReader but after a quick glance at the documentation I would say that the ReadString 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

              www.troschuetz.de

              Y 1 Reply Last reply
              0
              • S Stefan Troschuetz

                As said I'm not used to the XmlTextReader but after a quick glance at the documentation I would say that the ReadString 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

                www.troschuetz.de

                Y Offline
                Y Offline
                Yustme
                wrote on last edited by
                #7

                Hi, That did the trick. Thanks!

                1 Reply Last reply
                0
                • Y Yustme

                  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.

                  C Offline
                  C Offline
                  Colin Angus Mackay
                  wrote on last edited by
                  #8

                  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

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups