linq to XML as a gridview datasource
I'm trying to get all the ema开发者_如何学Cil nodes for the customers in the sample xml and binding it to a grid. I can't seemt to get past the linq query!
Sample XML:
<group>
<customer>
<email>testing@testing.com></email>
</customer>
<customer>
<email>testing2@testing.com</email>
</customer>
</group>
var query = from result in xml.Elements("customer")
select new
{
email = xml.Element("email").Value
};
gridview1.DataSource = query;
gridview1.DataBind();
Elements() will only get you direct children, therefore, if your xml variable is an XDocument, its only direct children (according to the little sample) are group elements.
Try:
var query = from result in xml.Descendants("customer")
select new { email = result.Element("email").Value };
精彩评论