上接稳扎稳打Silverlight(19) - 2.0通信之调用REST服务,处理JSON格式, XML格式, RSS/ATOM格式的数据...

2、调用 REST 服务,返回 XML 数据
REST.cs(WCF创建的REST服务)
InBlock.gif using System; 
InBlock.gif using System.Linq; 
InBlock.gif using System.Runtime.Serialization; 
InBlock.gif using System.ServiceModel; 
InBlock.gif using System.ServiceModel.Activation; 
InBlock.gif 
InBlock.gif using System.ServiceModel.Web; 
InBlock.gif using System.Collections.Generic; 
InBlock.gif using System.Text; 
InBlock.gif using System.IO; 
InBlock.gif 
/// <summary> 
/// 提供 REST 服务的类 
/// 注:Silverlight只支持 GET 和 POST 
/// </summary> 
InBlock.gif[ServiceContract] 
InBlock.gif[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 
InBlock.gif public  class REST 
InBlock.gif
InBlock.gif         /// <summary> 
InBlock.gif         /// 用于演示返回 XML(对象) 的 REST 服务 
InBlock.gif         /// </summary> 
InBlock.gif         /// <param name="name"></param> 
InBlock.gif         /// <returns></returns> 
InBlock.gif        [OperationContract] 
InBlock.gif        [WebGet(UriTemplate =  "User/{name}/xml", ResponseFormat = WebMessageFormat.Xml)] 
InBlock.gif         public User HelloXml( string name) 
InBlock.gif        { 
InBlock.gif                 return  new User { Name = name, DayOfBirth =  new DateTime(1980, 2, 14) }; 
InBlock.gif        } 
InBlock.gif 
InBlock.gif         /// <summary> 
InBlock.gif         /// 用于演示返回 XML(集合) 的 REST 服务 
InBlock.gif         /// </summary> 
InBlock.gif         /// <returns></returns> 
InBlock.gif        [OperationContract] 
InBlock.gif        [WebGet(UriTemplate =  "Users/xml", ResponseFormat = WebMessageFormat.Xml)] 
InBlock.gif         public List<User> HelloXml2() 
InBlock.gif        { 
InBlock.gif                 return  new List<User>    
InBlock.gif                {    
InBlock.gif                         new User(){ Name =  "webabcd01", DayOfBirth =  new DateTime(1980, 1, 1) }, 
InBlock.gif                         new User(){ Name =  "webabcd02", DayOfBirth =  new DateTime(1980, 2, 2) }, 
InBlock.gif                         new User(){ Name =  "webabcd03", DayOfBirth =  new DateTime(1980, 3, 3) }, 
InBlock.gif                }; 
InBlock.gif        } 
InBlock.gif
 
Xml.xaml
<UserControl x:Class="Silverlight20.Communication.Xml" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
        <StackPanel HorizontalAlignment="Left" Width="600"> 

                <TextBox x:Name="txtMsgXml" Margin="5" /> 
                <TextBox x:Name="txtMsgXml2" Margin="5" /> 

        </StackPanel> 
</UserControl>
 
Xml.xaml.cs
InBlock.gif using System; 
InBlock.gif using System.Collections.Generic; 
InBlock.gif using System.Linq; 
InBlock.gif using System.Net; 
InBlock.gif using System.Windows; 
InBlock.gif using System.Windows.Controls; 
InBlock.gif using System.Windows.Documents; 
InBlock.gif using System.Windows.Input; 
InBlock.gif using System.Windows.Media; 
InBlock.gif using System.Windows.Media.Animation; 
InBlock.gif using System.Windows.Shapes; 
InBlock.gif 
InBlock.gif using System.Xml.Linq; 
InBlock.gif using System.IO; 
InBlock.gif 
InBlock.gif namespace Silverlight20.Communication 
InBlock.gif
InBlock.gif         public partial  class Xml : UserControl 
InBlock.gif        { 
InBlock.gif                 public Xml() 
InBlock.gif                { 
InBlock.gif                        InitializeComponent(); 
InBlock.gif 
InBlock.gif                         // 演示如何处理 XML(对象) 
InBlock.gif                        XmlDemo(); 
InBlock.gif 
InBlock.gif                         // 演示如何处理 XML(集合) 
InBlock.gif                        XmlDemo2(); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 /// <summary> 
InBlock.gif                 /// 演示如何处理 XML(对象) 
InBlock.gif                 /// </summary> 
InBlock.gif                 void XmlDemo() 
InBlock.gif                { 
InBlock.gif                         // REST 服务的 URL 
InBlock.gif                        Uri uri =  new Uri( "http://localhost:3036/REST.svc/User/webabcd/xml", UriKind.Absolute); 
InBlock.gif 
InBlock.gif                        // 实例化 WebClient 
InBlock.gif                        System.Net.WebClient client = new System.Net.WebClient(); 
InBlock.gif 
InBlock.gif                        client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(xml_DownloadStringCompleted); 
InBlock.gif                        client.DownloadStringAsync(uri); 
InBlock.gif 
InBlock.gif                        txtMsgXml.Text = "读取 XML(对象) 数据中。。。"
InBlock.gif                } 
InBlock.gif 
InBlock.gif                void xml_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) 
InBlock.gif                { 
InBlock.gif                        if (e.Error != null
InBlock.gif                        { 
InBlock.gif                                // 发生错误的话,则打印出来 
InBlock.gif                                txtMsgXml.Text = e.Error.ToString(); 
InBlock.gif                                return
InBlock.gif                        } 
InBlock.gif 
InBlock.gif                        // 将获得的字符串转换为 XML(对象) 
InBlock.gif                        var buffer = System.Text.Encoding.UTF8.GetBytes(e.Result); 
InBlock.gif                        var ms = new MemoryStream(buffer); 
InBlock.gif 
InBlock.gif                        XElement xmlObject = XElement.Load(ms); 
InBlock.gif 
InBlock.gif                        txtMsgXml.Text = e.Result + "\r\n"
InBlock.gif                        XNamespace ns = "http://webabcd.cnblogs.com/"; 
InBlock.gif                        txtMsgXml.Text += string.Format("姓名: {0}, 生日: {1}"
InBlock.gif                                (string)xmlObject.Element(ns + "Name"), 
InBlock.gif                                ((DateTime)xmlObject.Element(ns + "DayOfBirth")).ToString("yyyy-MM-dd")); 
InBlock.gif 
InBlock.gif                        /*    
InBlock.gif                         * 总结: 
InBlock.gif                         * XElement - 表示一个 XML 元素 
InBlock.gif                         *         XElement.Element - XML 元素内的 XML 元素 
InBlock.gif                         *         XElement.Attribute - XML 元素内的 XML 属性 
InBlock.gif                         *         XElement.Load(Stream) - 使用指定流创建一个 XElement 对象 
InBlock.gif                         *         XElement.Parse(String) - 解析指定的 XML 字符串为一个 XElement 对象 
InBlock.gif                         * XAttribute - 表示一个 XML 属性 
InBlock.gif                         */
 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                /// <summary> 
InBlock.gif                /// 演示如何处理 XML(集合) 
InBlock.gif                /// </summary> 
InBlock.gif                void XmlDemo2() 
InBlock.gif                { 
InBlock.gif                        // REST 服务的 URL 
InBlock.gif                        Uri uri = new Uri("http://localhost:3036/REST.svc/Users/xml", UriKind.Absolute); 
InBlock.gif 
InBlock.gif                        // 实例化 WebClient 
InBlock.gif                        System.Net.WebClient client = new System.Net.WebClient(); 
InBlock.gif 
InBlock.gif                        client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(xml2_DownloadStringCompleted); 
InBlock.gif                        client.DownloadStringAsync(uri); 
InBlock.gif 
InBlock.gif                        txtMsgXml2.Text = "读取 XML(集合) 数据中。。。"
InBlock.gif                } 
InBlock.gif 
InBlock.gif                void xml2_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) 
InBlock.gif                { 
InBlock.gif                        if (e.Error != null
InBlock.gif                        { 
InBlock.gif                                // 发生错误的话,则打印出来 
InBlock.gif                                txtMsgXml2.Text = e.Error.ToString(); 
InBlock.gif                                return
InBlock.gif                        } 
InBlock.gif 
InBlock.gif                        // 将获得的字符串转换为 XML(集合) 
InBlock.gif                        XDocument xmlObject = XDocument.Parse(e.Result); 
InBlock.gif 
InBlock.gif                        txtMsgXml2.Text = e.Result + "\r\n"
InBlock.gif                        XNamespace ns = "http://webabcd.cnblogs.com/"; 
InBlock.gif                        var obj = from p in xmlObject.Elements(ns + "ArrayOfUser").Elements(ns + "User"
InBlock.gif                                            where p.Element(ns + "Name").Value == "webabcd02" 
InBlock.gif                                            select new { Name = (string)p.Element(ns + "Name"), DayOfBirth = (DateTime)p.Element(ns + "DayOfBirth") }; 
InBlock.gif                         
InBlock.gif                        txtMsgXml2.Text += string.Format("姓名: {0}, 生日: {1}"
InBlock.gif                                obj.First().Name, 
InBlock.gif                                obj.First().DayOfBirth.ToString("yyyy-MM-dd")); 
InBlock.gif 
InBlock.gif 
InBlock.gif                        /*    
InBlock.gif                         * 总结: 
InBlock.gif                         * LINQ to XML 相当的方便 
InBlock.gif                         */
 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
 
3、调用 REST 服务,返回 Rss/Atom 数据
Proxy.aspx.cs(返回指定的url地址的内容的服务)
InBlock.gif using System; 
InBlock.gif using System.Collections.Generic; 
InBlock.gif using System.Linq; 
InBlock.gif using System.Web; 
InBlock.gif using System.Web.UI; 
InBlock.gif using System.Web.UI.WebControls; 
InBlock.gif 
InBlock.gif public partial  class Proxy : System.Web.UI.Page 
InBlock.gif
InBlock.gif         protected  void Page_Load( object sender, EventArgs e) 
InBlock.gif        { 
InBlock.gif                 // 获取某个 url 地址的 html 并在页面上输出 
InBlock.gif 
InBlock.gif                 string url = Request.QueryString[ "url"]; 
InBlock.gif 
InBlock.gif                System.Net.WebClient client =  new System.Net.WebClient(); 
InBlock.gif                client.Encoding = System.Text.Encoding.UTF8; 
InBlock.gif 
InBlock.gif                Response.Write(client.DownloadString(url)); 
InBlock.gif                Response.End(); 
InBlock.gif        } 
InBlock.gif}
 
RssAtom.xaml
<UserControl x:Class="Silverlight20.Communication.RssAtom" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    
        xmlns:x="http://schemas.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>
 
RssAtom.xaml.cs
InBlock.gif using System; 
InBlock.gif using System.Collections.Generic; 
InBlock.gif using System.Linq; 
InBlock.gif using System.Net; 
InBlock.gif using System.Windows; 
InBlock.gif using System.Windows.Controls; 
InBlock.gif using System.Windows.Documents; 
InBlock.gif using System.Windows.Input; 
InBlock.gif using System.Windows.Media; 
InBlock.gif using System.Windows.Media.Animation; 
InBlock.gif using System.Windows.Shapes; 
InBlock.gif 
InBlock.gif using System.Xml; 
InBlock.gif using System.IO; 
InBlock.gif using System.ServiceModel.Syndication; 
InBlock.gif 
InBlock.gif namespace Silverlight20.Communication 
InBlock.gif
InBlock.gif         public partial  class RssAtom : UserControl 
InBlock.gif        { 
InBlock.gif                 public RssAtom() 
InBlock.gif                { 
InBlock.gif                        InitializeComponent(); 
InBlock.gif 
InBlock.gif                         // 演示如何处理 Rss/Atom 
InBlock.gif                        RssDemo(); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 /// <summary> 
InBlock.gif                 /// 演示如何处理 Rss/Atom 
InBlock.gif                 /// </summary> 
InBlock.gif                 void RssDemo() 
InBlock.gif                { 
InBlock.gif                         // 让一个代理页面去请求相关的 Rss/Atom(如果用Silverlight直接去请求,则需要在目标域的根目录下配置策略文件) 
InBlock.gif                        Uri uri =  new Uri( "http://localhost:3036/Proxy.aspx?url=http://webabcd.cnblogs.com/rss", UriKind.Absolute); 
InBlock.gif 
InBlock.gif                        // 实例化 WebClient 
InBlock.gif                        System.Net.WebClient client = new System.Net.WebClient(); 
InBlock.gif 
InBlock.gif                        client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(rss_DownloadStringCompleted); 
InBlock.gif                        client.DownloadStringAsync(uri); 
InBlock.gif 
InBlock.gif                        txtMsgRss.Text = "读取 RSS 数据中。。。"
InBlock.gif                } 
InBlock.gif 
InBlock.gif                void rss_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) 
InBlock.gif                { 
InBlock.gif                        if (e.Error != null
InBlock.gif                        { 
InBlock.gif                                // 发生错误的话,则打印出来 
InBlock.gif                                txtMsgRss.Text = e.Error.ToString(); 
InBlock.gif                                return
InBlock.gif                        } 
InBlock.gif 
InBlock.gif                        // 将获得的字符串转换为 XmlReader 
InBlock.gif                        var buffer = System.Text.Encoding.UTF8.GetBytes(e.Result); 
InBlock.gif                        var ms = new MemoryStream(buffer); 
InBlock.gif                        XmlReader reader = XmlReader.Create(ms); 
InBlock.gif 
InBlock.gif                        // 从指定的 XmlReader 中加载,以生成 SyndicationFeed 
InBlock.gif                        SyndicationFeed feed = SyndicationFeed.Load(reader); 
InBlock.gif 
InBlock.gif                        // 设置 list 的数据源为 Rss/Atom 的项集合(SyndicationFeed.Items) 
InBlock.gif                        list.ItemsSource = feed.Items; 
InBlock.gif                        txtMsgRss.Text = e.Result + "\r\n"
InBlock.gif                } 
InBlock.gif 
InBlock.gif                private void list_SelectionChanged(object sender, SelectionChangedEventArgs e) 
InBlock.gif                { 
InBlock.gif                        // 设置 detail 的数据上下文为 Rss/Atom 的指定项(SyndicationItem) 
InBlock.gif                        detail.DataContext = list.SelectedItem as SyndicationItem; 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 




     本文转自webabcd 51CTO博客,原文链接:http://blog.51cto.com/webabcd/343113 ,如需转载请自行联系原作者
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值