public string Author { get; set; } } 再添加一个Blog类,它有一个返回类型为IQueryable
实现服务,让它继承于泛型的WebDataService,并且设置访问权限。
public class BlogDataService : WebDataService
现在还看不到所有的Posts,我们可以在地址栏中输入http://localhost:8081/BlogDataService.svc/Posts,浏览器会默认为Feed打开,可以查看源代码,将会看到所有内容,XML内容如下(只列出片段):
当然还可以进行其他的查询,使用filter和orderby等,如
http://localhost:8081/BlogDataService.svc/Posts?$filter=Id eq 1&$orderby=Id,这里不在介绍。至此我们的数据服务端就算完成了。下面再实现客户端,XAML不再贴出来,大家可以参考前面的几篇文章,使用WebClient获取数据,返回的结果是一个XML文件: private void UserControl_Loaded(object sender, RoutedEventArgs e) { Uri uri = new Uri(\); WebClient client = new WebClient(); client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted); client.OpenReadAsync(uri); } void client_OpenReadCompleted(object sender,OpenReadCompletedEventArgs e) { if (e.Error == null) { } } 我们可以使用LINQ to XML进行数据的读取,在Silverlight项目中建立一个Post类,跟上面的Post类一样,然后使用LINQ to XML读取:
XmlReader reader = XmlReader.Create(e.Result); XDocument postdoc = XDocument.Load(reader); XNamespace xmlns = \; XNamespace ads = \; var posts = from x in postdoc.Descendants(xmlns + \) select new Post { Id = int.Parse(x.Descendants(ads + \).First().Value), Title = x.Descendants(ads + \).First().Value, Author = x.Descendants(ads + \).First().Value }; Posts.ItemsSource = posts; 完成的代码如下所示:
private void UserControl_Loaded(object sender, RoutedEventArgs e) { Uri uri = new Uri(\); WebClient client = new WebClient(); client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted); client.OpenReadAsync(uri); } void client_OpenReadCompleted(object sender,OpenReadCompletedEventArgs e) { if (e.Error == null) {
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库一步一步学习Silverlight之数据与通信篇(8)在线全文阅读。
相关推荐: