Tuesday, April 22, 2008

Load xml into XElement and loop through xElement

string url = Server.MapPath("Books.xml");
XElement x = XElement.Load(url, LoadOptions.PreserveWhitespace);
IEnumerable<xelement> authors = x.Descendants("author");
foreach (XElement author in authors)
{
Response.Write(author.Value + "<br />");
}
Here is XML file :

<?xml version="1.0" encoding="utf-8" ?><books><book><title>LINQ in Action</title><author>Fabrice Marguerie</author><author>Steve Eichert</author><author>Jim Wooley</author><publisher>Manning</publisher></book></books>

No comments: