Much More Linq to Xml

by sunil ravulapalli /8. September 2011 17:52 /c# /Comments (0)
  
    100
    
    2
    
  

This is how the query looks:

WebReportData webReportData =
(
	from xml in data.Elements("xml")
	select new WebReportData
	{
		Distance = (from element in xml.Descendants("element") 
					where xml.Element("element").Attribute("name").Value == "distance" 
					select element).First().Value,
		Email = (from element in xml.Descendants("element") 
					 where xml.Element("element").Attribute("name").Value == "email" 
					 select element).First().Value,
		Education = (from element in xml.Descendants("element") 
					 where xml.Element("element").Attribute("name").Value == "education" 
					 select element).First().Value,
		Phone = (from element in xml.Descendants("element") 
					 where xml.Element("element").Attribute("name").Value == "phone" 
					 select element).First().Value
	}
).First();

The classes look like this:

public class WebReportData
{
	public string Distance { get; set; }
	public string Email { get; set; }
	public string Education { get; set; }
	public string Phone { get; set; }
}
blog comments powered by Disqus