silverlight
发布时间:2022-05-04 发布网站:大佬教程 code.js-code.com
大佬教程收集整理的这篇文章主要介绍了稳扎稳打Silverlight(19) - 2.0通信之调用REST服务,处理JSON格式, XML格式, RSS/ATOM格式的数据,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
概述
[索引页] [源码下载] 稳扎稳打Silverlight(1
9) - 2.0通信之调用REST服务,处理JSON格式, XML格式, RSS/ATOM格式的数据 作者: webabcd 介绍 Silverlight 2.0 调用REST服务,处理JSON格式, XML格式, RSS/ATOM格式的数据 通过 Syste
m.Net.WebClient 类调用 REST 服务 通过 S
[索引页]
[源码下载]
稳扎稳打Silverlight(1
9) - 2.0通信之
调用REST服务,处理JSON格式,XML格式,
RSS/ATOM格式的数据
作者:
webabcd
介绍
Silverlight 2.0
调用REST服务,处理JSON格式,
RSS/ATOM格式的数据
通过 Sy
stem.Net.WebClient 类
调用 REST 服务
通过 Sy
stem.Json 命名控件下的类处理 JSON 数据
通过 Sy
stem.Xm
l.Linq 命名空间下的类(LINQ to XML)处理 XML 数据
通过 Sy
stem.
serviceMode
l.Syn
Dication 命名空间下的类处理
RSS/ATOM 数据
在线DEMO
http://www.cnblogs.com/webabcd/archive/2008/10/09/1307486.html
示例
1、
调用 REST 服务,返回 JSON 数据
REST.cs(WCF创建的REST服务)
using
System;
using
System.Linq;
using
System.Runtime.serialization;
using
System.serviceModel;
using
System.serviceModel.Activation;
using
System.serviceModel.Web;
using
System.Collections.Generic;
using
System.Text;
using
System.IO;
/**/
/// <sumMary>
/// 提供 rEST 服务的类
/// 注:Silverlight只支持 GET 和 POST
/// </sumMary>
[serviceContract]
[AspNetCompatibilityrequirements(requirementsMode
=
AspNetCompatibilityrequirementsMode.Allowed)]
public
class
rEST
{
/**//// <sumMary>
/// 用于演示返回 JSON(对象) 的 rEST 服务
/// </sumMary>
/// <param NAME="name"></param>
/// <returns></returns>
[OperationContract]
[WebGet(UriTemplate = "User/{name}/json", responseFormat = Webmessageformat.Json)]
public User HelloJson(String Name)
{
@H_301_318@
return new User { NAME = NAME, DayOfBirth = new datetiR_913_11845@e(1980, 2, 14) };
}
/**//// <sumMary>
/// 用于演示返回 JSON(集合) 的 rEST 服务
/// </sumMary>
/// <returns></returns>
[OperationContract]
[WebGet(UriTemplate = "Users/json", responseFormat = Webmessageformat.Json)]
public List<User> HelloJson2()
{
return new List<User>
@H_772_633@{
new User(){ NAME = "webabcd01", 1, 1) },
new User(){ NAME = "webabcd02", 2) },
new User(){ NAME = "webabcd03", 3, 3) },
};
}
}
Json.xaml
<
UserControl
x:Class
="Silverlight20.Communication.Json"
xmlns
="http://scheR_913_11845@as.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x
="http://scheR_913_11845@as.microsoft.com/winfx/2006/xaml"
>
<
StackPanel
HorizontalAlignment
="Left"
Width
="600"
>
<
TextBox
x:Name
="txtmsgJson"
Margin
="5"
/>
<
TextBox
x:Name
="txtmsgJson2"
Margin
="5"
/>
</
StackPanel
>
</
UserControl
>
Json.xam
l.cs
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Net;
using
System.Windows;
using
System.Windows.Controls;
using
System.Windows.Documents;
using
System.Windows.Input;
using
System.Windows.Media;
using
System.Windows.Media.Animation;
using
System.Windows.Shapes;
using
System.IO;
namespace
Silverlight20.Communication
{
public partial class Json : UserControl
{
public Json()
{
initializeComponent();
// 演示如何处理 JSON(对象)
JsonDemo();
// 演示如何处理 JSON(集合)
JsonDemo2();
}
/**//// <sumMary>
/// 演示如何处理 JSON(对象)
/// </sumMary>
void JsonDemo()
{
// rEST 服务的 URL
Uri uri = new Uri("http://localhost:3036/REST.svc/User/webabcd/json", UriKind.AbsolutE);
// 实例化 WebClient
System.Net.WebClient client = new System.Net.WebClient();
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(json_DownloadStringCompleted);
client.DownloadStringAsync(uri);
txtmsgJson.Text = "读取 JSON(对象) 数据中。。。";
}
void json_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs E)
{
if (e.Error != null)
{
// 发生错误的话,则打印出来
txtmsgJson.Text = e.Error.ToString();
return;
}
// 将获得的字符串转换为 JSON(对象)
var buffer = System.Text.Encoding.UTF8.GetBytes(e.Result);
var ms = new MemoryStream(buffer);
var jsonObject = System.Json.JsonObject.Load(ms) as System.Json.JsonObject;
txtmsgJson.Text = e.Result + "/r/n";
// 解析 JSON(对象)
txtmsgJson.Text += String.Format("姓名: {0}, 生日: {1}",
(String)jsonObject["Name"],
((datetiR_913_11845@E)jsonObject["DayOfBirth"]).ToString("yyyy-MM-dd"));
/**//*
* 总结:
* JsonObject - 一个具有零或多个 key-value 对的无序集合。继承自抽象类 JsonValue
* JsonObject.Load(Stream) - 将指定的字符串流反序列化为 JSON 对象(CLR可用的类型)
* JsonObject[key] - JsonObject 索引器,@L_674_56@ JsonObject 的指定key的value
* JsonObject.ContainsKey(key) - JsonObject 对象内是否具有指定的key
*/
}
/**//// <sumMary>
/// 演示如何处理 JSON(集合)
/// </sumMary>
void JsonDemo2()
{
// rEST 服务的 URL
Uri uri = new Uri("http://localhost:3036/REST.svc/Users/json", UriKind.AbsolutE);
// 实例化 WebClient
System.Net.WebClient client = new System.Net.WebClient();
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(json2_DownloadStringCompleted);
client.DownloadStringAsync(uri);
txtmsgJson2.Text = "读取 JSON(集合) 数据中。。。";
}
void json2_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs E)
{
if (e.Error != null)
@H_403_1742@{
// 发生错误的话,则打印出来
txtmsgJson2.Text = e.Error.ToString();
return;
}
// 将获得的字符串转换为 JSON(集合)
var buffer = System.Text.Encoding.UTF8.GetBytes(e.Result);
var ms = new MemoryStream(buffer);
var jsonArray = System.Json.JsonArray.Load(ms) as System.Json.JsonArray;
txtmsgJson2.Text = e.Result + "/r/n";
txtmsgJson2.Text += String.Format("姓名: {0},
(String)jsonArray.First()["Name"],
((datetiR_913_11845@E)jsonArray.Single(p => p["Name"] == "webabcd02")["DayOfBirth"]).ToString("yyyy-MM-dd"));
/**//*
* 总结:
* JsonArray - 一个具有零或多个 JsonValue(抽象类,JsonObject和JsonArray都继承自此类) 对象的有序序列
* JsonArray.Load(Stream) - 将指定的字符串流反序列化为 JSON 对象(CLR可用的类型)
* JsonArraY[index] - JsonArray 索引器,@L_674_56@ JsonArray 的指定索引的 JsonValue
* JsonArray 支持 LINQ
*/
}
}
}
2、
调用 REST 服务,返回 XML 数据
REST.cs(WCF创建的REST服务)
using
System;
using
System.Linq;
using
System.Runtime.serialization;
using
System.serviceModel;
using
System.serviceModel.Activation;
using
System.serviceModel.Web;
using
System.Collections.Generic;
using
System.Text;
using
System.IO;
/**/
/// <sumMary>
/// 提供 rEST 服务的类
/// 注:Silverlight只支持 GET 和 POST
/// </sumMary>
[serviceContract]
[AspNetCompatibilityrequirements(requirementsMode
=
AspNetCompatibilityrequirementsMode.Allowed)]
public
class
rEST
{
@H_759_2673@
/**//// <sumMary>
/// 用于演示返回 XML(对象) 的 rEST 服务
/// </sumMary>
/// <param NAME="name"></param>
/// <returns></returns>
[OperationContract]
[WebGet(UriTemplate = "User/{name}/xml", responseFormat = Webmessageformat.Xml)]
public User HelloXml(String Name)
{
return new User { NAME = NAME, 14) };
}
/**//// <sumMary>
/// 用于演示返回 XML(集合) 的 rEST 服务
/// </sumMary>
/// <returns></returns>
[OperationContract]
[WebGet(UriTemplate = "Users/xml", responseFormat = Webmessageformat.Xml)]
public List<User> HelloXml2()
{
return new List<User>
{
new User(){ NAME = "webabcd01",
new User(){ NAME = "webabcd02",
new User(){ NAME = "webabcd03",
};
}
}
Xm
l.xaml
<
UserControl
x:Class
="Silverlight20.Communication.Xml"
xmlns
="http://scheR_913_11845@as.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x
="http://scheR_913_11845@as.microsoft.com/winfx/2006/xaml"
>
<
StackPanel
HorizontalAlignment
="Left"
Width
="600"
>
<
TextBox
x:Name
="txtmsgXml"
Margin
="5"
/>
<
TextBox
x:Name
="txtmsgXml2"
Margin
="5"
/>
</
StackPanel
>
</
UserControl
>
Xm
l.xam
l.cs
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Net;
using
System.Windows;
using
System.Windows.Controls;
using
System.Windows.Documents;
using
System.Windows.Input;
using
System.Windows.Media;
using
System.Windows.Media.Animation;
using
System.Windows.Shapes;
using
System.Xml.Linq;
using
System.IO;
namespace
Silverlight20.Communication
{
public partial class Xml : UserControl
{
public Xml()
{
initializeComponent();
// 演示如何处理 XML(对象)
XmlDemo();
// 演示如何处理 XML(集合)
XmlDemo2();
}
@H_19_3675@
/**//// <sumMary>
/// 演示如何处理 XML(对象)
/// </sumMary>
void XmlDemo()
{
// rEST 服务的 URL
Uri uri = new Uri("http://localhost:3036/REST.svc/User/webabcd/xml", UriKind.AbsolutE);
// 实例化 WebClient
System.Net.WebClient client = new System.Net.WebClient();
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(xml_DownloadStringCompleted);
client.DownloadStringAsync(uri);
txtmsgXml.Text = "读取 XML(对象) 数据中。。。";
}
void xml_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs E)
{
if (e.Error != null)
{
// 发生错误的话,则打印出来
txtmsgXml.Text = e.Error.ToString();
return;
}
// 将获得的字符串转换为 XML(对象)
var buffer = System.Text.Encoding.UTF8.GetBytes(e.Result);
var ms = new MemoryStream(buffer);
XElement xmlObject = XElement.Load(ms);
txtmsgXml.Text = e.Result + "/r/n";
XNamespace ns = "http://webabcd.cnblogs.com/";
txtmsgXml.Text += String.Format("姓名: {0},
(String)xmlObject.Element(ns + "Name"),
((datetiR_913_11845@E)xmlObject.Element(ns + "DayOfBirth")).ToString("yyyy-MM-dd"));
/**//*
* 总结:
* XElement - 表示一个 XML 元素
* XElement.Element - XML 元素内的 XML 元素
* XElement.Attribute - XML 元素内的 XML 属性
* XElement.Load(Stream) - 使用指定流创建一个 XElement 对象
* XElement.Parse(String) - 解析指定的 XML 字符串为一个 XElement 对象
* XAttribute - 表示一个 XML 属性
*/
}
/**//// <sumMary>
/// 演示如何处理 XML(集合)
/// </sumMary>
void XmlDemo2()
@H_867_4326@
{
// rEST 服务的 URL
Uri uri = new Uri("http://localhost:3036/REST.svc/Users/xml", UriKind.AbsolutE);
// 实例化 WebClient
System.Net.WebClient client = new System.Net.WebClient();
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(xml2_DownloadStringCompleted);
client.DownloadStringAsync(uri);
txtmsgXml2.Text = "读取 XML(集合) 数据中。。。";
}
void xml2_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs E)
@H_985_4500@@H_668_4507@{
if (e.Error != null)
{
// 发生错误的话,则打印出来
txtmsgXml2.Text = e.Error.ToString();
return;
}
// 将获得的字符串转换为 XML(集合)
XDocument xmlObject = XDocument.Parse(e.Result);
txtmsgXml2.Text = e.Result + "/r/n";
XNamespace ns = "http://webabcd.cnblogs.com/";
var obj = from p in xmlObject.Elements(ns + "ArrayOfUser").Elements(ns + "User")
where p.Element(ns + "Name").Value == "webabcd02"
SELEct new { NAME = (String)p.Element(ns + "Name"), DayOfBirth = (datetiR_913_11845@E)p.Element(ns + "DayOfBirth") };
txtmsgXml2.Text += String.Format("姓名: {0},
obj.First().Name,
obj.First().DayOfBirth.ToString("yyyy-MM-dd"));
@H_674_4871@/**//*
* 总结:
* LINQ to XML 相当的方便
*/
}
}
}
3、
调用 REST 服务,返回
RSS/Atom 数据
Proxy.aspx.cs(返回指定的
URL地址的
内容的服务)
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
public
partial
class
Proxy : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs E)
{
// @L_674_56@某个 url 地址的 html 并在页面上输出
String url = request.QueryString["url"];
System.Net.WebClient client = new System.Net.WebClient();
client.Encoding = System.Text.Encoding.UTF8;
response.Write(client.DownloadString(url));
response.End();
}
}
RSSAto
m.xaml
<
UserControl
x:Class
="Silverlight20.Communication.RSSAtom"
xmlns
="http://scheR_913_11845@as.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x
="http://scheR_913_11845@as.microsoft.com/winfx/2006/xaml"
>
<
StackPanel
HorizontalAlignment
="Left"
>
<
TextBox
x:Name
="txtmsgRSS"
Width
="600"
Margin
="5"
/>
<
StackPanel
Orientation
="Horizontal"
>
<
ListBox
x:Name
="list"
Width
="300"
Margin
="5"
SELEctionChanged
="list_SELEctionChanged"
>
<
ListBox.ItemTemplate
>
<
DataTemplate
>
<
TextBlock
Text
="
{Binding title.Text}
"
></
TextBlock
>
</
DataTemplate
>
</
ListBox.ItemTemplate
>
</
ListBox
>
<
TextBlock
x:Name
="detail"
Width
="300"
Margin
="5"
text
="
{Binding SumMary.Text}
"
textwrapping
="Wrap"
/>
</
StackPanel
>
</
StackPanel
>
</
UserControl
>
RSSAto
m.xam
l.cs
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Net;
using
System.Windows;
using
System.Windows.Controls;
using
System.Windows.Documents;
using
System.Windows.Input;
using
System.Windows.Media;
using
System.Windows.Media.Animation;
using
System.Windows.Shapes;
using
System.Xml;
using
System.IO;
using
System.serviceModel.SynDication;
namespace
Silverlight20.Communication
{
public partial class RSSAtom : UserControl
{
public RSSAtom()
{
initializeComponent();
// 演示如何处理 RSS/Atom
RSSDemo();
}
@H_868_5874@
@H_618_5879@
/**//// <sumMary>
/// 演示如何处理 RSS/Atom
/// </sumMary>
void RSSDemo()
{
// 让一个代理页面去请求相关的 RSS/Atom(如果用Silverlight直接去请求,则需要在目标域的根目录下配置策略文件)
Uri uri = new Uri("http://localhost:3036/Proxy.aspx?url=http://webabcd.cnblogs.com/RSS", UriKind.AbsolutE);
// 实例化 WebClient
System.Net.WebClient client = new System.Net.WebClient();
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(RSS_DownloadStringCompleted);
client.DownloadStringAsync(uri);
txtmsgRSS.Text = "读取 RSS 数据中。。。";
}
void RSS_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs E)
@H_616_6110@{
if (e.Error != null)
{
// 发生错误的话,则打印出来
txtmsgRSS.Text = e.Error.ToString();
return;
}
// 将获得的字符串转换为 XmlReader
var buffer = System.Text.Encoding.UTF8.GetBytes(e.Result);
var ms = new MemoryStream(buffer);
XmlReader reader = XmlReader.Create(ms);
// 从指定的 XmlReader 中加载,以生成 SynDicationFeed
SynDicationFeed Feed = SynDicationFeed.Load(reader);
// 设置 list 的数据源为 RSS/Atom 的项集合(SynDicationFeed.Items)
list.Itemssource = Feed.Items;
txtmsgRSS.Text = e.Result + "/r/n";
}
private void list_SELEctionChanged(object sender, SELEctionChangedEventArgs E)
{
// 设置 detail 的数据上下文为 RSS/Atom 的指定项(SynDicationItem)
detail.DataContext = list.SELEctedItem as SynDicationItem;
}
}
}
OK
[源码下载]
大佬总结
以上是大佬教程为你收集整理的稳扎稳打Silverlight(19) - 2.0通信之调用REST服务,处理JSON格式, XML格式, RSS/ATOM格式的数据全部内容,希望文章能够帮你解决稳扎稳打Silverlight(19) - 2.0通信之调用REST服务,处理JSON格式, XML格式, RSS/ATOM格式的数据所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。