First of all you have to wrap the code into a root element. I used <books>
<?xml version="1.0" encoding="utf-8" ?>
<books>
<book id=1>
<name>abc</name>
<title>xyz</title>
</book>
<book id=2>
<name>pqr</name>
<title>lmn</title>
</book>
</books>
You can use an XmlReader, but I traverse all nodes. It gives me flexibility
with selecting only elements I need:
XmlDocument doc = new XmlDocument();
doc.Load("doc.x ml");
XmlElement root = doc.DocumentEle ment;
XmlNodeList nodes = root.SelectNode s("/books"); // You can filter elements
here using XPath
foreach (XmlNode node in nodes)
{
string name = node["name"].InnerText;
string title = node["title"].InnerText;
// Do whatever you want
Console.Write(" name\t{0}\title \t{1}", name, title);
}
"Praveen Naregal" <pg*******@hotm ail.com> wrote in message
news:u2******** ******@TK2MSFTN GP11.phx.gbl...
hello all,
I have a problem. I have an XML file like below one.I want to loop through
node by node and store or display the data .
Can anybody give me a sample piece of c# code?
Thanks & Regards,
Praveen
<?xml version="1.0" encoding="utf-8" ?>
<book id=1>
<name>abc</name>
<title>xyz</title>
</book>
<book id=2>
<name>pqr</name>
<title>lmn</title>
</book>