read XML, get values en names.

Posted on 2006-03-31
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!

Question by:EnolaKotrotsos
    LVL 52

    Accepted Solution

    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);

    LVL 18

    Expert Comment

    by:Ravi Singh
    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) {
             = 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!
    LVL 5

    Expert Comment

    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.
    LVL 1

    Author Comment

    thank you all very much!
    you are livesavers

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Course: HTML5 Specialist

    HTML5 development skills are critical to all developers. HTML5 is the foundation to almost any development process. That's why business, design studios, development shops and other organizations need HTML5 developers. Get your foot in the door as a HTML5 specialist.

    Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
    I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
    Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now