代码
void Page_Loaded(object sender, RoutedEventArgs e)
{
WebClient client=new WebClient ();
client .OpenReadCompleted+=new OpenReadCompletedEventHandler(client_OpenReadCompleted);
client .OpenReadAsync(new Uri ("Data.xml" ,UriKind.Relative));
}
void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
XElement xml=XElement .Load (XmlReader.Create (e.Result ));
var query=
from i in xml.DescendantsAndSelf("item")
let cid=(string)i.Ancestors ("customer").First().Attribute ("id")
group (decimal) i.Attribute ("price")*(int)i.Attribute ("quantity")
by cid
into groupedSales
let totalSales=groupedSales .Sum()
orderby totalSales descending
select new SalesData {
Customer =groupedSales.Key ,
TotalSales =totalSales
};
LayoutRoot .DataContext =query .ToList ();
}