现在我们的服务端就完成了,现在我们可以在浏览器中访问BlogDataService.svc,应该可以看到如下界面:

附件:
您所在的用户组无法下载或查看附件现在还看不到所有的Posts,我们可以在地址栏中输入http://localhost:8081/BlogDataService.svc/Posts,浏览器会默认为Feed打开,可以查看源代码,将会看到所有内容,XML内容如下(只列出片段):
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<feed xml:base="http://localhost:8081/BlogDataService.svc/" ......>
<id>http://localhost:8081/BlogDataService.svc/Posts</id>
<updated />
<title>Posts</title>
<link rel="self" href="Posts" title="Posts" />
<entry adsm:type="TerryLee.SilverlightWithDataServiceDemoWeb.Post">
<id>http://localhost:8081/BlogDataService.svc/Posts(1)</id>
<updated />
<title />
<author>
<name />
</author>
<link rel="edit" href="Posts(1)" title="Post" />
<content type="application/xml">
<ads:Id adsm:type="Int32">1</ads:Id>
<ads:Title>一步一步学Silverlight 2系列(13):数据与通信之WebRequest</ads:Title>
<ads:Author>TerryLee</ads:Author>
</content>
</entry>
如果要查看某一条文章的内容,可以输入http://localhost:8081/BlogDataService.svc/Posts(2)进行查看,如下图所示。

附件:
您所在的用户组无法下载或查看附件当然还可以进行其他的查询,使用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("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)
{
}
}
我们可以使用LINQ to XML进行数据的读取,在Silverlight项目中建立一个Post类,跟上面的Post类一样,然后使用LINQ to XML读取:
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;
完成的代码如下所示:
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了解不多,有错误的地方还请大家斧正,你可以从这里下载示例代码:

附件:
您所在的用户组无法下载或查看附件。