学Silverlight 2系列(17):数据与通信之ADO.NET Data Services
2008-10-03 11:35:40 来源:WEB开发网private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
Uri uri = new Uri("http://localhost:8081/BlogDataService.svc/Posts");
WebClient client = new WebClient();
client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
client.OpenReadAsync(uri);
}
void client_OpenReadCompleted(object sender,OpenReadCompletedEventArgs e)
{
if (e.Error == null)
{
XmlReader reader = XmlReader.Create(e.Result);
XDocument postdoc = XDocument.Load(reader);
XNamespace xmlns = "http://www.w3.org/2005/Atom";
XNamespace ads = "http://schemas.microsoft.com/ado/2007/08/dataweb";
var posts = from x in postdoc.Descendants(xmlns + "entry")
select new Post
{
Id = int.Parse(x.Descendants(ads + "Id").First().Value),
Title = x.Descendants(ads + "Title").First().Value,
Author = x.Descendants(ads + "Author").First().Value
};
Posts.ItemsSource = posts;
}
}
完整的示例就到这里了,运行后的结果与前面的一样。
结束语
本文简单介绍了在Silverlight 2调用ADO.NET Data Services,由于对ADO.NET Data Services了解不多,有错误的地方还请大家斧正,你可以下载示例代码。
Tags:Silverlight 系列 数据
编辑录入:爽爽 [复制链接] [打 印]更多精彩
赞助商链接