| re: looping through XML nodes
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.xml");
XmlElement root = doc.DocumentElement;
XmlNodeList nodes = root.SelectNodes("/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" <pgnaregal@hotmail.com> wrote in message
news:u2EdskO4DHA.2680@TK2MSFTNGP11.phx.gbl...[color=blue]
> 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>
>
>
>[/color] |