<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      穩扎穩打Silverlight(19) - 2.0通信之調用REST服務,處理JSON格式, XML格式, RSS/ATOM格式的數據

      [索引頁]
      [源碼下載]


      穩扎穩打Silverlight(19) - 2.0通信之調用REST服務,處理JSON格式, XML格式, RSS/ATOM格式的數據


      作者:webabcd


      介紹
      Silverlight 2.0 調用REST服務,處理JSON格式, XML格式, RSS/ATOM格式的數據
          通過 System.Net.WebClient 類調用 REST 服務
          通過 System.Json 命名控件下的類處理 JSON 數據
          通過 System.Xml.Linq 命名空間下的類(LINQ to XML)處理 XML 數據
          通過 System.ServiceModel.Syndication 命名空間下的類處理 RSS/ATOM 數據


      在線DEMO
      http://www.rzrgm.cn/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)
          
      {
              
      return new User { Name = name, DayOfBirth = new DateTime(1980214) };
          }


          
      /// <summary>
          
      /// 用于演示返回 JSON(集合) 的 REST 服務
          
      /// </summary>
          
      /// <returns></returns>

          [OperationContract]
          [WebGet(UriTemplate 
      = "Users/json", ResponseFormat = WebMessageFormat.Json)]
          
      public List<User> HelloJson2()
          
      {
              
      return new List<User> 
              

                  
      new User(){ Name = "webabcd01", DayOfBirth = new DateTime(198011) },
                  
      new User(){ Name = "webabcd02", DayOfBirth = new DateTime(198022) },
                  
      new User(){ Name = "webabcd03", DayOfBirth = new DateTime(198033) },
              }
      ;
          }

      }


      Json.xaml
      <UserControl x:Class="Silverlight20.Communication.Json"
          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="txtMsgJson" Margin="5" />
              
      <TextBox x:Name="txtMsgJson2" Margin="5" /> 
              
          
      </StackPanel>
      </UserControl>

      Json.xaml.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"],
                      ((DateTime)jsonObject[
      "DayOfBirth"]).ToString("yyyy-MM-dd"));

                  
      /* 
                   * 總結:
                   * JsonObject - 一個具有零或多個 key-value 對的無序集合。繼承自抽象類 JsonValue
                   *     JsonObject.Load(Stream) - 將指定的字符串流反序列化為 JSON 對象(CLR可用的類型)
                   *     JsonObject[key] - JsonObject 索引器,獲取 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)
                  
      {
                      
      // 發生錯誤的話,則打印出來
                      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}, 生日: {1}",
                      (
      string)jsonArray.First()["Name"],
                      ((DateTime)jsonArray.Single(p 
      => p["Name"== "webabcd02")["DayOfBirth"]).ToString("yyyy-MM-dd"));

                  
      /* 
                   * 總結:
                   * JsonArray - 一個具有零或多個 JsonValue(抽象類,JsonObject和JsonArray都繼承自此類) 對象的有序序列
                   *     JsonArray.Load(Stream) - 將指定的字符串流反序列化為 JSON 對象(CLR可用的類型)
                   *     JsonArray[index] - JsonArray 索引器,獲取 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
      {
          
      /// <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, DayOfBirth = new DateTime(1980214) };
          }


          
      /// <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", DayOfBirth = new DateTime(198011) },
                  
      new User(){ Name = "webabcd02", DayOfBirth = new DateTime(198022) },
                  
      new User(){ Name = "webabcd03", DayOfBirth = new DateTime(198033) },
              }
      ;
          }

      }


      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
      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();
              }


              
      /// <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}, 生日: {1}",
                      (
      string)xmlObject.Element(ns + "Name"),
                      ((DateTime)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()
              
      {
                  
      // 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)
              
      {
                  
      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 = (DateTime)p.Element(ns + "DayOfBirth") };
                  
                  txtMsgXml2.Text 
      += string.Format("姓名: {0}, 生日: {1}",
                      obj.First().Name,
                      obj.First().DayOfBirth.ToString(
      "yyyy-MM-dd"));


                  
      /* 
                   * 總結:
                   * 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)
          
      {
              
      // 獲取某個 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();
          }

      }


      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
      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();
              }


              
      /// <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)
              
      {
                  
      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
      [源碼下載]
      posted @ 2008-12-04 08:24  webabcd  閱讀(7953)  評論(51)    收藏  舉報
      主站蜘蛛池模板: 亚洲一区二区三区丝袜| 欧美老少配性行为| 国产露脸无套对白在线播放| 亚洲成亚洲成网| 日韩有码中文字幕av| 国产亚洲精品久久久久久青梅 | 高清国产亚洲精品自在久久| 亚洲 制服 丝袜 无码| 亚洲精品日韩在线观看| 国内自拍视频在线一区| 亚洲一区二区三区激情在线| 国产69成人精品视频免费| 国产av不卡一区二区| 玩弄放荡人妻少妇系列| 日本一区二区三区小视频| 高潮潮喷奶水飞溅视频无码| 人妻人人澡人人添人人爽人人玩 | 性动态图无遮挡试看30秒| 韩国午夜福利片在线观看| 麻豆一区二区三区蜜桃免费| 国产精品综合色区av| 亚洲国产精品一二三区| 免费一级黄色好看的国产| 久久天天躁狠狠躁夜夜躁2o2o| 插入中文字幕在线一区二区三区 | 亚洲色欲在线播放一区| 91福利一区二区三区| 亚洲精品日本一区二区| 国产精品九九久久精品女同 | www插插插无码免费视频网站| 无码AV动漫精品一区二区免费| 国产乱码1卡二卡3卡四卡5| 国产一区二区在线观看粉嫩 | 久久精品国产再热青青青| 国产成人综合亚洲欧美日韩 | 欧美成本人视频免费播放 | 亚洲高清WWW色好看美女| 中文字幕va一区二区三区| 米奇影院888奇米色99在线| 夜色福利站WWW国产在线视频| 国产视频一区二区三区视频|