在Silverlight中读取ADO.NET数据服务的两种方式
2010-03-10 11:32
417 查看
1。 数据服务的返回数据格式
注意,没有办法通过添加引用的方式调用该服务,估计有安全方面的一些考虑吧
2。因为ADO.NET数据服务是可以直接通过http请求实现,所以下面两个代码都可以完成工作
首先准备地址和查询,并且直接通过webclient来发起请求即可
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
另外,因为数据服务返回的数据其实是基于ATOM格式的,所以还可以通过下面的方式解析
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
大家可以看出两个方法其实有相似之处,因为都涉及到了XML的解析。但他们也有类似的一个问题,就是如果我们的类型属性很多,那么那些代码很繁琐。不是吗?其实是有规律可循的。我一般会用下面的方法来解决问题
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
这样一来,我们的代码就可以简化成下面这样,看起来不错对吧
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
注意,没有办法通过添加引用的方式调用该服务,估计有安全方面的一些考虑吧
2。因为ADO.NET数据服务是可以直接通过http请求实现,所以下面两个代码都可以完成工作
首先准备地址和查询,并且直接通过webclient来发起请求即可
Uri svc = new Uri(Application.Current.Host.Source, "../NorthwindService.svc/Customers()?$orderby=CustomerID desc"); WebClient proxy = new WebClient(); proxy.OpenReadCompleted += new OpenReadCompletedEventHandler(proxy_OpenReadCompleted); proxy.OpenReadAsync(svc);
然后在proxy_OpenReadCompleted方法中编写如下代码即可
XNamespace d = "http://schemas.microsoft.com/ado/2007/08/dataservices"; XNamespace xmlns = "http://www.w3.org/2005/Atom"; XDocument doc = XDocument.Load(e.Result); var query = from entry in doc.Root.Elements(xmlns + "entry") select new Customer() { CustomerID = entry.Descendants(d + "CustomerID").First().Value, CompanyName = entry.Descendants(d + "CompanyName").First().Value, Country = entry.Descendants(d + "Country").First().Value, City = entry.Descendants(d + "City").First().Value }; foreach (var item in query) { oc.Add(item); }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
另外,因为数据服务返回的数据其实是基于ATOM格式的,所以还可以通过下面的方式解析
XNamespace d = "http://schemas.microsoft.com/ado/2007/08/dataservices"; XNamespace xmlns = "http://www.w3.org/2005/Atom"; #region //读取到数据进行解析 XmlReader reader = XmlReader.Create(e.Result); SyndicationFeed feed = SyndicationFeed.Load(reader); foreach (var item in feed.Items) { XmlSyndicationContent c = (XmlSyndicationContent)item.Content; XmlDictionaryReader r = c.GetReaderAtContent(); XElement element = XElement.Load(r); oc.Add(new Customer() { CustomerID = element.Descendants(d + "CustomerID").First().Value, CompanyName = element.Descendants(d + "CompanyName").First().Value, Country = element.Descendants(d + "Country").First().Value, City = element.Descendants(d + "City").First().Value }); r.Close(); oc.Add(LoadXml(r)); } reader.Close();
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
大家可以看出两个方法其实有相似之处,因为都涉及到了XML的解析。但他们也有类似的一个问题,就是如果我们的类型属性很多,那么那些代码很繁琐。不是吗?其实是有规律可循的。我一般会用下面的方法来解决问题
/// /// 这是一个静态方法,可以将一段XML转换为一个对象 /// /// /// /// public static T LoadXml(XmlReader reader) where T:new() { XNamespace d = "http://schemas.microsoft.com/ado/2007/08/dataservices"; XElement element = XElement.Load(reader); T result = new T(); foreach (var p in typeof(T).GetProperties()) { Type pType = p.PropertyType; string pName = p.Name; string pValue = element.Descendants(d + pName).First().Value; p.SetValue(result, Convert.ChangeType(pValue, pType, null), null); } reader.Close(); return result; } /// /// 这是一个静态方法,可以将一段XML转换为一个对象 /// /// /// /// public static T LoadXml(XElement element) where T : new() { XNamespace d = "http://schemas.microsoft.com/ado/2007/08/dataservices"; T result = new T(); foreach (var p in typeof(T).GetProperties()) { Type pType = p.PropertyType; string pName = p.Name; string pValue = element.Descendants(d + pName).First().Value; p.SetValue(result, Convert.ChangeType(pValue, pType, null), null); } return result; }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
这样一来,我们的代码就可以简化成下面这样,看起来不错对吧
var query = from entry in doc.Root.Elements(xmlns + "entry") select LoadXml(entry);
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
相关文章推荐
- 在Silverlight中读取ADO.NET数据服务的两种方式
- 两个Repeater嵌套实现动态菜单(ado.net+sql和xml+Linq两种读取数据方式)
- 关于ADO.NET下数据的两种访问方式
- 稳扎稳打Silverlight(28) - 2.0通信之调用ADO.NET Data Services(数据服务)
- ado.net读取数据的两种方法
- ADO 与ADO.NET两种数据访问方式区别
- ADO 与ADO.NET两种数据访问方式区别?
- 初识ADO.net-访问数据的两种方式
- 稳扎稳打Silverlight(28) - 2.0通信之调用ADO.NET Data Services(数据服务)
- 初识ADO.net-访问数据的两种方式
- ADO与ADO.NET两种数据访问方式区别
- ADO 与ADO.NET两种数据访问方式区别
- 稳扎稳打Silverlight(28) - 2.0通信之调用ADO.NET Data Services(数据服务)
- ADO 与ADO.NET两种数据访问方式区别。
- ADO 与ADO.NET两种数据访问方式区别
- 两种数据访问方式:从ADO 到ADO.NET
- ADO 与ADO.NET两种数据访问方式区别
- ADO与ADO.NET两种数据访问方式区别
- 一步一步学Silverlight 2系列(17):数据与通信之ADO.NET Data Services
- ADO.NET基础实战 从文件读取数据插入到数据库