read XML, get values en names.

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



LVL 1
EnolaKotrotsosAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Carl TawnSystems and Integration DeveloperCommented:
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();
        }
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ravi SinghSenior Software EngineerCommented:
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!
0
TertioptusCommented:
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.
0
EnolaKotrotsosAuthor Commented:
thank you all very much!
you are livesavers
Marco
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.