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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Reading an XML file

Reading an XML file

Scheduled Pinned Locked Moved C#
questionxml
12 Posts 4 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
    yonidebest
    wrote on last edited by
    #1

    I looked all over the web for an easy way to read an XML file, with no success. Most places I found used the XmlTextReader.Read() method, which I don't find easy to use if I dont wanna go thru the whole file. This is what I am looking for. Say I have the following XML file: 10,000,000 1.2 7,000,000 2.4 and I would like to access Miami's population element (and get the 7,000,000 string) something like this (I have invent the names of the methods - the final question follows these exampls): string PopulationOfMiami = xmlfile["Miami"].population; or perhaps: string PopulationOfMiami = xmlfile["Miami"].population.value; or perhaps: string PopulationOfMiami = xmlfile["Miami"].ElementSon("population").value; or perhaps: string PopulationOfMiami = xmlfile.findNameAttribute("Miami").population.value; I want access to my XML file in a similar form in one of the above fashioned-way. Does anyone know of real methods and class that work like above? I hope I explained myself OK :doh:. Thanks, Yoni

    G K M 3 Replies Last reply
    0
    • Y yonidebest

      I looked all over the web for an easy way to read an XML file, with no success. Most places I found used the XmlTextReader.Read() method, which I don't find easy to use if I dont wanna go thru the whole file. This is what I am looking for. Say I have the following XML file: 10,000,000 1.2 7,000,000 2.4 and I would like to access Miami's population element (and get the 7,000,000 string) something like this (I have invent the names of the methods - the final question follows these exampls): string PopulationOfMiami = xmlfile["Miami"].population; or perhaps: string PopulationOfMiami = xmlfile["Miami"].population.value; or perhaps: string PopulationOfMiami = xmlfile["Miami"].ElementSon("population").value; or perhaps: string PopulationOfMiami = xmlfile.findNameAttribute("Miami").population.value; I want access to my XML file in a similar form in one of the above fashioned-way. Does anyone know of real methods and class that work like above? I hope I explained myself OK :doh:. Thanks, Yoni

      G Offline
      G Offline
      gnadeem
      wrote on last edited by
      #2

      you may consider using XPath. have a look at this. http://www.codeproject.com/soap/myXPath.asp[^]

      1 Reply Last reply
      0
      • Y yonidebest

        I looked all over the web for an easy way to read an XML file, with no success. Most places I found used the XmlTextReader.Read() method, which I don't find easy to use if I dont wanna go thru the whole file. This is what I am looking for. Say I have the following XML file: 10,000,000 1.2 7,000,000 2.4 and I would like to access Miami's population element (and get the 7,000,000 string) something like this (I have invent the names of the methods - the final question follows these exampls): string PopulationOfMiami = xmlfile["Miami"].population; or perhaps: string PopulationOfMiami = xmlfile["Miami"].population.value; or perhaps: string PopulationOfMiami = xmlfile["Miami"].ElementSon("population").value; or perhaps: string PopulationOfMiami = xmlfile.findNameAttribute("Miami").population.value; I want access to my XML file in a similar form in one of the above fashioned-way. Does anyone know of real methods and class that work like above? I hope I explained myself OK :doh:. Thanks, Yoni

        K Offline
        K Offline
        Kent Sharkey
        wrote on last edited by
        #3

        It looks like you should actually be using XmlDocument, rather than XmlTextReader. That will let you access your data similar to what you're looking for. XmlDocument doc = new XmlDocument(); doc.LoadXml(yourXML); String PopOfMiami = doc.SelectSingleNode("/cities/city[@name='Miami']/population").InnerText; Alternately, you can create an XML serializer, which would give you syntax closer to what you're looking for.

        -------------- TTFN - Kent

        Y 1 Reply Last reply
        0
        • Y yonidebest

          I looked all over the web for an easy way to read an XML file, with no success. Most places I found used the XmlTextReader.Read() method, which I don't find easy to use if I dont wanna go thru the whole file. This is what I am looking for. Say I have the following XML file: 10,000,000 1.2 7,000,000 2.4 and I would like to access Miami's population element (and get the 7,000,000 string) something like this (I have invent the names of the methods - the final question follows these exampls): string PopulationOfMiami = xmlfile["Miami"].population; or perhaps: string PopulationOfMiami = xmlfile["Miami"].population.value; or perhaps: string PopulationOfMiami = xmlfile["Miami"].ElementSon("population").value; or perhaps: string PopulationOfMiami = xmlfile.findNameAttribute("Miami").population.value; I want access to my XML file in a similar form in one of the above fashioned-way. Does anyone know of real methods and class that work like above? I hope I explained myself OK :doh:. Thanks, Yoni

          M Offline
          M Offline
          meeram395
          wrote on last edited by
          #4

          Hi, There is another way which you can read the values of XML file. Given below the code. 1. In the XML file, add the lines as below: CityNY name="New York" population=10,000,000 growth=1.2 CityMi name="Miami" population=7,000,000 growth=2.4 2. Open a new class called ReadXMLConfig 3. use the namespace, using System.Xml; 4. Declare the data members as follows : public class ReadXMLConfig { XMLDocument xmlDoc; private string NYName; private string NYpop; private string NYgrowth; private string MiName; private string Mipop; private string Migrowth; } 5. Type the following in the constructor area: xmlDoc=new XmlDocument // in the constructor NYName = null; NYpop = null; NYgrowth = null; MiName=null; Mipop=null; migrowth=null; 6. Create a function called LoadXML and type as follows: public void LoadXML() { xmlDoc.Load("XMLFilename"); XmlNodeList elemList; XmlElement elem; elemList = xmlDoc.GetElementsByTagName("CityNY"); elem = (XmlElement)elemList[0]; NYName = elem.GetAttribute("name"); NYpop = elem.GetAttribute("population"); NYgrowth = elem.GetAttribte("growth"); elemList = xmlDoc.GetElementsByTagName("CityMi"); elem = (XmlElement)elemList[0]; MiName = elem.GetAttribute("name"); Mipop = elem.GetAttribute("population"); Migrowth = elem.GetAttribte("growth"); } Call this load function wherever you want. Hope this will helps. Cheers -- modified at 4:24 Tuesday 12th December, 2006

          Meeram395

          Y 1 Reply Last reply
          0
          • K Kent Sharkey

            It looks like you should actually be using XmlDocument, rather than XmlTextReader. That will let you access your data similar to what you're looking for. XmlDocument doc = new XmlDocument(); doc.LoadXml(yourXML); String PopOfMiami = doc.SelectSingleNode("/cities/city[@name='Miami']/population").InnerText; Alternately, you can create an XML serializer, which would give you syntax closer to what you're looking for.

            -------------- TTFN - Kent

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

            Thanks - this looks like what I was looking for. Problem is that I have a problem: My XML is a file, not a string variable. i.e. this doesn't work: XmlDocument doc = new XmlDocument(); doc.LoadXml(@"c:\myfile.xml"); String PopOfMiami = doc.SelectSingleNode("/cities/city[@name='Miami']/population").InnerText; I looked around for a way around this, trying to give LoadXml a XmlReader and XmlTextReader, but nothing. How can I load my XML file and then do the select? Thanks, Yoni

            K 1 Reply Last reply
            0
            • M meeram395

              Hi, There is another way which you can read the values of XML file. Given below the code. 1. In the XML file, add the lines as below: CityNY name="New York" population=10,000,000 growth=1.2 CityMi name="Miami" population=7,000,000 growth=2.4 2. Open a new class called ReadXMLConfig 3. use the namespace, using System.Xml; 4. Declare the data members as follows : public class ReadXMLConfig { XMLDocument xmlDoc; private string NYName; private string NYpop; private string NYgrowth; private string MiName; private string Mipop; private string Migrowth; } 5. Type the following in the constructor area: xmlDoc=new XmlDocument // in the constructor NYName = null; NYpop = null; NYgrowth = null; MiName=null; Mipop=null; migrowth=null; 6. Create a function called LoadXML and type as follows: public void LoadXML() { xmlDoc.Load("XMLFilename"); XmlNodeList elemList; XmlElement elem; elemList = xmlDoc.GetElementsByTagName("CityNY"); elem = (XmlElement)elemList[0]; NYName = elem.GetAttribute("name"); NYpop = elem.GetAttribute("population"); NYgrowth = elem.GetAttribte("growth"); elemList = xmlDoc.GetElementsByTagName("CityMi"); elem = (XmlElement)elemList[0]; MiName = elem.GetAttribute("name"); Mipop = elem.GetAttribute("population"); Migrowth = elem.GetAttribte("growth"); } Call this load function wherever you want. Hope this will helps. Cheers -- modified at 4:24 Tuesday 12th December, 2006

              Meeram395

              Y Offline
              Y Offline
              yonidebest
              wrote on last edited by
              #6

              Thanks, but I can't change the XML so easily - I found a VB code on the net that converts an excel file to an xml file. It isn't flexible. the city tags represent a row in an excel. can't rename them. Thanks anyway, I might use this in the future. Yoni

              1 Reply Last reply
              0
              • Y yonidebest

                Thanks - this looks like what I was looking for. Problem is that I have a problem: My XML is a file, not a string variable. i.e. this doesn't work: XmlDocument doc = new XmlDocument(); doc.LoadXml(@"c:\myfile.xml"); String PopOfMiami = doc.SelectSingleNode("/cities/city[@name='Miami']/population").InnerText; I looked around for a way around this, trying to give LoadXml a XmlReader and XmlTextReader, but nothing. How can I load my XML file and then do the select? Thanks, Yoni

                K Offline
                K Offline
                Kent Sharkey
                wrote on last edited by
                #7

                If the XML is in a file use doc.Load(path) instead.

                -------------- TTFN - Kent

                Y 1 Reply Last reply
                0
                • K Kent Sharkey

                  If the XML is in a file use doc.Load(path) instead.

                  -------------- TTFN - Kent

                  Y Offline
                  Y Offline
                  yonidebest
                  wrote on last edited by
                  #8

                  Thanks. New problem :-) I get a "XmlException: '?' is an unexpected token. Expecting white space" exception. doh. Any ideas? Thanks again, Yoni

                  K 1 Reply Last reply
                  0
                  • Y yonidebest

                    Thanks. New problem :-) I get a "XmlException: '?' is an unexpected token. Expecting white space" exception. doh. Any ideas? Thanks again, Yoni

                    K Offline
                    K Offline
                    Kent Sharkey
                    wrote on last edited by
                    #9

                    What does your XML look like? -- modified at 14:52 Tuesday 12th December, 2006 As in, it's not just the six lines above, is it?

                    -------------- TTFN - Kent

                    Y 1 Reply Last reply
                    0
                    • K Kent Sharkey

                      What does your XML look like? -- modified at 14:52 Tuesday 12th December, 2006 As in, it's not just the six lines above, is it?

                      -------------- TTFN - Kent

                      Y Offline
                      Y Offline
                      yonidebest
                      wrote on last edited by
                      #10

                      No, it isn't just six lines. It's similar tho - many more cities and many more elements. Now that you mention it - it has a first line: Could it be this? -- modified at 21:29 Tuesday 12th December, 2006

                      K 1 Reply Last reply
                      0
                      • Y yonidebest

                        No, it isn't just six lines. It's similar tho - many more cities and many more elements. Now that you mention it - it has a first line: Could it be this? -- modified at 21:29 Tuesday 12th December, 2006

                        K Offline
                        K Offline
                        Kent Sharkey
                        wrote on last edited by
                        #11

                        No, that's a fairly standard XML declaration. However, I'm guessing there's some high-ASCII character somewhere in that document, or something sitting in the wrong place. Does the error message come with a position (something along the lines of "The expected token is 'whitespace'. Line 1, position 40. )

                        -------------- TTFN - Kent

                        Y 1 Reply Last reply
                        0
                        • K Kent Sharkey

                          No, that's a fairly standard XML declaration. However, I'm guessing there's some high-ASCII character somewhere in that document, or something sitting in the wrong place. Does the error message come with a position (something along the lines of "The expected token is 'whitespace'. Line 1, position 40. )

                          -------------- TTFN - Kent

                          Y Offline
                          Y Offline
                          yonidebest
                          wrote on last edited by
                          #12

                          ahhh Thanks! Indeed - it did have a position - I looked it up in the XML and found a name with a quote in it :-) I'll escape it. Many thanks, Yoni

                          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