We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

read XML, get values en names.

Medium Priority
316 Views
Last Modified: 2013-11-19
afternoon all.

I have this XML file

<data>
  <top>
       <name>Marco</name>
       <adres>Kandinskystraat</adres>
       <etc>etcetc</etc>
  </top>
</data>

i need to be able to get all the elements between <top></top> it can be 10 lines long, it can be 600 lines long. i need to loop through all of these and get
the element names and it's respective value as say values for the variables xmlElemName and xmlElemValue. (they are all string values)
thanks alot!
Marco



Comment
Watch Question

Senior Systems and Integration Developer
CERTIFIED EXPERT
Commented:
Heres a quick sample, using a console app, of how to loop through all the child nodes:
 
      static void Main()
      {
            XmlDocument doc = new XmlDocument();
            doc.Load("C:\\Test.xml");

            XmlNode root = doc.SelectSingleNode("//top");

            foreach (XmlNode node in root.ChildNodes)
            {
                Console.WriteLine(node.Name + ": " + node.InnerText);
            }

            Console.ReadLine();
        }

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Ravi SinghSenior Software Engineer

Commented:
Hi, assuming your storing that data in a class like:

      class Top {
            public string name;
            public string adres;
            public string etc;

            public Top(string name, string adres, string etc) {
                  this.name = name;
                  this.adres = adres;
                  this.etc = etc;
            }
      }

Then the following method will return an arraylist of Top objects with the data from the xml:

            private ArrayList GetData() {
            
                  ArrayList topList = new ArrayList();
                  XmlDocument xmlDoc = new XmlDocument();
                  xmlDoc.Load(@"C:\Temp\Sample.xml"); // Change path

                  XmlNodeList topNodeList = xmlDoc.DocumentElement.SelectNodes("/data/top");
                  foreach (XmlNode topNode in topNodeList) {
                        topList.Add(new Top(topNode.SelectSingleNode("name").InnerText,
                              topNode.SelectSingleNode("adres").InnerText,
                              topNode.SelectSingleNode("etc").InnerText));
                  }
                  
                  return topList;
            }

Hope this helps!
I suggest using ADO.NET.

If you are using the VS.NET IDE:

1. Open a sample XML that has your desired schema in VS.NET.

2. Right click on the file and generate an XML schema out of it.

3. Add that schema to your project.

4. Open the schema, right click on it and generate a Dataset.

5. In your code, instantiate the dataset and call ".ReadXML(Sample.xml)" form that new instance

You should be able to get your values from the "Top" table in the dataset.

Author

Commented:
thank you all very much!
you are livesavers
Marco
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.