Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


read XML, get values en names.

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

Question by:EnolaKotrotsos
LVL 52

Accepted Solution

Carl Tawn earned 2000 total points
ID: 16341001
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
ID: 16341030
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!

Expert Comment

ID: 16343909
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 Comment

ID: 16348708
thank you all very much!
you are livesavers

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

564 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