您的位置:首页 > Web前端 > JavaScript

2.0通信之调用REST服务,处理JSON格式, XML格式, RSS/ATOM格式的数据

2009-06-08 22:01 756 查看
介绍
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
/article/4589581.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;

[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class REST

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

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;

[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class REST

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

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

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐