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


read XML, get values en names.

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

I have this XML file


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!

Watch Question

Senior Systems and Integration Developer
Heres a quick sample, using a console app, of how to loop through all the child nodes:
      static void Main()
            XmlDocument doc = new XmlDocument();

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

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


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

Ask the Experts
Ravi SinghSenior Software Engineer

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,
                  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.


thank you all very much!
you are livesavers
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.


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.