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 using XmlReader

Reading Xml using XmlReader

Scheduled Pinned Locked Moved C#
tutorialxmllearning
8 Posts 2 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.
  • P Offline
    P Offline
    Patrick Klug
    wrote on last edited by
    #1

    Hi folks, I am a bit puzzled how to use the System.Xml.XmlReader to read data :doh: To give a simple example I want to read a Xml file like this: Peter Alen in a object with the same properties... what I did was something like this: ... Person person = new Person(); while (reader.Read()) { if (reader.Name=="Name") { person.Name = reader.ReadElementContentAsString(); } if (reader.Sirname == "Sirname") { person.Sirname = reader.ReadElementContentAsString(); } } ... of course simplified, the real thing is a bit more complicated... this does work if the Xml is exactly the same way I outlined above but if, for example, the Name tag comes after the Sirname tag the code doesn't work anymore because the reader.ReadElementContentAsString() is advancing the reader and the reader.Read() as well thus one element is simply not handled by the code in the while loop... I hope I explained it well enough :^) any suggestions welcome! TIA Pakl

    C 1 Reply Last reply
    0
    • P Patrick Klug

      Hi folks, I am a bit puzzled how to use the System.Xml.XmlReader to read data :doh: To give a simple example I want to read a Xml file like this: Peter Alen in a object with the same properties... what I did was something like this: ... Person person = new Person(); while (reader.Read()) { if (reader.Name=="Name") { person.Name = reader.ReadElementContentAsString(); } if (reader.Sirname == "Sirname") { person.Sirname = reader.ReadElementContentAsString(); } } ... of course simplified, the real thing is a bit more complicated... this does work if the Xml is exactly the same way I outlined above but if, for example, the Name tag comes after the Sirname tag the code doesn't work anymore because the reader.ReadElementContentAsString() is advancing the reader and the reader.Read() as well thus one element is simply not handled by the code in the while loop... I hope I explained it well enough :^) any suggestions welcome! TIA Pakl

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      If you don't have a proper schema, you're better off using an XmlDocument or XmlDataDocument, and XPath

      Christian Graus - C++ MVP 'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert

      P 1 Reply Last reply
      0
      • C Christian Graus

        If you don't have a proper schema, you're better off using an XmlDocument or XmlDataDocument, and XPath

        Christian Graus - C++ MVP 'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert

        P Offline
        P Offline
        Patrick Klug
        wrote on last edited by
        #3

        hmm, alright... then I will switch to XPath for reading and use XmlWriter for writing... thanks just another thought... I searched for any trick I could use to keep using XmlReader... maybe something like this...

        bool read = false;
        do
        {
        read = false;
        if (reader.Name=="Person")
        {
        person = reader.ReadElementContentAsString();
        read = true;
        }
        ..... other stuff comes here...
        }
        while (read || reader.Read())

        but of course I don't want to create a mess when I forget to set the read = true.... doesn't seem to fit so I will use XPath...

        C 1 Reply Last reply
        0
        • P Patrick Klug

          hmm, alright... then I will switch to XPath for reading and use XmlWriter for writing... thanks just another thought... I searched for any trick I could use to keep using XmlReader... maybe something like this...

          bool read = false;
          do
          {
          read = false;
          if (reader.Name=="Person")
          {
          person = reader.ReadElementContentAsString();
          read = true;
          }
          ..... other stuff comes here...
          }
          while (read || reader.Read())

          but of course I don't want to create a mess when I forget to set the read = true.... doesn't seem to fit so I will use XPath...

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          The core issue is that you don't have a defined schema.  It would be better if you did.

          Christian Graus - C++ MVP 'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert

          P 1 Reply Last reply
          0
          • C Christian Graus

            The core issue is that you don't have a defined schema.  It would be better if you did.

            Christian Graus - C++ MVP 'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert

            P Offline
            P Offline
            Patrick Klug
            wrote on last edited by
            #5

            I understand. Just a question on that topic. If I have a defined schema, does this mean that those tags have to be in the same order all the time. I always thought that it is 'correct' to place tags in any order in the file.

            C 1 Reply Last reply
            0
            • P Patrick Klug

              I understand. Just a question on that topic. If I have a defined schema, does this mean that those tags have to be in the same order all the time. I always thought that it is 'correct' to place tags in any order in the file.

              C Offline
              C Offline
              Christian Graus
              wrote on last edited by
              #6

              No, a schema defines the order as well.

              Christian Graus - C++ MVP 'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert

              P 1 Reply Last reply
              0
              • C Christian Graus

                No, a schema defines the order as well.

                Christian Graus - C++ MVP 'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert

                P Offline
                P Offline
                Patrick Klug
                wrote on last edited by
                #7

                hmm, alright, probably I am not understanding that so well after all :doh: how exactly would it help me, if I had a schema... btw. thanks for your time

                C 1 Reply Last reply
                0
                • P Patrick Klug

                  hmm, alright, probably I am not understanding that so well after all :doh: how exactly would it help me, if I had a schema... btw. thanks for your time

                  C Offline
                  C Offline
                  Christian Graus
                  wrote on last edited by
                  #8

                  It wouldn't, directly, unless you needed to validate the documents coming in.  But, it's very uncommon for a program to not care what order the nodes are in, or expect them to differ.

                  Christian Graus - C++ MVP 'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert

                  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