Introduction:
In this article,i am going to explain about how to loop through the xml nodes using asp.net
Main:
We can easily retrive/print the xml node value using the below code.
XDocument xDoc = XDocument.Parse(
@"<Customers>
<Customer>
<ID>0001</ID>
<FirstName>Harry</FirstName>
<LastName>L</LastName>
</Customer>
<Customer>
<ID>0002</ID>
<FirstName>Harry</FirstName>
<LastName>L</LastName>
</Customer>
</Customers>");
if (xDoc != null)
{
foreach (XNode xNode in xDoc.Descendants())
{
foreach (XElement xElement in xNode.Ancestors().Ancestors())
{
Console.WriteLine(xNode.ToString());
}
}
Console.ReadLine();
}
}
XDocument xDoc = XDocument.Parse( @"<Customers> <Customer> <ID>0001</ID> <FirstName>Harry</FirstName> <LastName>L</LastName> </Customer> <Customer> <ID>0002</ID> <FirstName>Harry</FirstName> <LastName>L</LastName> </Customer> </Customers>"); if (xDoc != null) { foreach (XNode xNode in xDoc.Descendants()) { foreach (XElement xElement in xNode.Ancestors().Ancestors()) { Console.WriteLine(xNode.ToString()); } } Console.ReadLine(); } } |
Conclusion:
Hope this helps,
Happy Coding.
My cousin recommended this blog and she was totally right keep up the fantastic work!
This nice summary assited me very much! Bookmarked your site, extremely interesting topics everywhere that I see here! I really like the information, thanks.
The educational article assited me very much! Bookmarked your website, extremely interesting topics everywhere that I see here! I appreciate the info, thank you.
The informative article assited me very much! Saved your blog, very excellent topics just about everywhere that I see here! I like the information, thanks.
Thanks very much for writing this awesome info! I am looking forward to reading more posts!