xml reader question

  • Thread starter Jeroen Ceuppens
  • Start date
J

Jeroen Ceuppens

Hi, below an example of a simple xml file
I want to get the name and id info out of each Node, what is the easiest way
to do that?



- <uit_te_voeren>
- <Node id="0">
<Name>File</Name>
<ID>10</ID>
</Node>
- <Node id="1">
<Name>View</Name>
<ID>2</ID>
</Node>
</uit_te_voeren>

Greetz
JC
 
S

Sandeep Prabhakar [MSFT]

This sample code should help you out.

using System;
using System.IO;
using System.Xml;

public class Sample
{
public static void Main()
{
XmlNodeReader reader = null;

try
{
//Create and load the XML document.
XmlDocument doc = new XmlDocument();
doc.LoadXml("<book>" +
"<title>Pride And Prejudice</title>" +
"<price>19.95</price>" +
"<misc/>" +
"</book>");

//Load the XmlNodeReader
reader = new XmlNodeReader(doc);

//Parse the XML and display the text content of each of the elements.
while (reader.Read())
{
if (reader.IsStartElement())
{
if (reader.IsEmptyElement)
Console.WriteLine("<{0}/>", reader.Name);
else
{
Console.Write("<{0}> ", reader.Name);
reader.Read(); //Read the start tag.
if (reader.IsStartElement()) //Handle nested elements.
Console.Write("\r\n<{0}>", reader.Name);
Console.WriteLine(reader.ReadString()); //Read the text content of
the element.
}
}
}

}

finally
{
if (reader != null)
reader.Close();
}
}

} // End class

Thanks,
Sandy


This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top