WCF Web Services + RSS Feed Reader + JavaScript (DOM) + DropDownList + Textbox 使用方法 (SOAP Server Side / SOAP Client Side – Return DataSet)

 
WCF Web Services + RSS Feed Reader + JavaScript (DOM) + DropDownList + Textbox 使用方法 (SOAP Server Side / SOAP Client Side – Return DataSet)
 
(SOAP Server Side)

   IService1.cs
 
   using System;
   using System.Collections.Generic;
   using System.Runtime.Serialization;
   using System.ServiceModel;
   using System.Data;
   using System.Xml;
   using System.ServiceModel.Web;
 
   namespace WcfService1
   {
 
      // 注意: 您可以使用 [重構] 功能表上的 [重新命名] 命令同時變更程式碼和組態檔中的介面名稱 "IService1"。
      [ServiceContract]
      public interface IService1
      {
 
         [OperationContract, WebGet(ResponseFormat = WebMessageFormat.Json)]
         RSSGroup ReturnNodeList();
 
         // TODO: 在此新增您的服務作業
      }
 
      [DataContract]
      public class RSSGroup
      {
         [DataMember]
         public String Title;
 
         [DataMember]
         public String Link;
 
         [DataMember]
         public DataSet RSSContent;
 
         public RSSGroup(String InputTitle, String InputLink, DataSet InputRSSContent)
         {
            Title = InputTitle;
            Link = InputLink;
            RSSContent = InputRSSContent;
         }
      }
 
   }
 

 

   Service1.svc.cs
 
   using System;
   using System.Collections.Generic;
   using System.Text;
   using System.Data.SqlClient;
   using System.ServiceProcess;
   using System.Data;
   using System.Web.UI.WebControls;
   using System.Xml;
 
   namespace WcfService1
   {
 
      // 注意: 您可以使用 [重構] 功能表上的 [重新命名] 命令同時變更程式碼、svc 和組態檔中的類別名稱 "Service1"。
      public class Service1 : IService1
      {
         public RSSGroup ReturnNodeList()
         {
 
            String FeedURL = "http://www.rthk.org.hk/rthk/news/rss/c_finance.xml";
 
            XmlDataSource XmlDataSource1 = new XmlDataSource();
 
            XmlDataSource1.DataFile = FeedURL;
 
            XmlDocument xmlDoc = XmlDataSource1.GetXmlDocument();
 
            XmlNodeList nodelist = xmlDoc.SelectNodes("/rss/channel");
 
            String HeadTitle = nodelist[0].SelectSingleNode("title").InnerText;
            String HeadLink = nodelist[0].SelectSingleNode("link").InnerText;
 
            DataSet ds = new DataSet();
 
            nodelist = xmlDoc.SelectNodes("/rss/channel/item");
 
            DataTable dt = new DataTable();
 
            dt.Columns.Add("Title");
            dt.Columns.Add("URL");
            dt.Columns.Add("Description");
 
            String Title;
            String Description;
            String URL;
 
            for (int i = 0; i < nodelist.Count; i++)
            {
 
               Title = nodelist[i].SelectSingleNode("title").InnerText;
               URL = nodelist[i].SelectSingleNode("link").InnerText;
               Description = nodelist[i].SelectSingleNode("description").InnerText;
 
               dt.Rows.Add(Title, URL, Description);
 
            }
 
            ds.Tables.Add(dt);
 
            return new RSSGroup(HeadTitle, HeadLink, ds);
 
         }
      }
   }
 

 
(SOAP Client Side – 需自行增加 Service References)

   Default.aspx
 
   <script type="text/javascript">
 
      function changetext() {
 
         var DropDownList = document.getElementById("MainContent_DropDownList1");
 
         for (var i = 0; i < (DropDownList.length); i++) {
            if (DropDownList.options[i].selected) {
               RSSContent = DropDownList.options[i].getAttribute("value");
            }
         }
 
         document.getElementById("MainContent_TextBox1").innerHTML = RSSContent;
 
      }
 
   </script>
 
   <asp:Label ID="Label1" runat="server" Text="Label" /><br /><br />
 
   <asp:DropDownList ID="DropDownList1" runat="server" Width="400px" onChange="changetext()" />
   <br /><br />
 
   <asp:TextBox ID="TextBox1" runat="server"
   Width="400px" Height="200px"
   ReadOnly="True" TextMode="MultiLine" />
 
   <br />
 

 

   Default.aspx.cs
 
   using System;
   using System.Collections.Generic;
   using System.Collections;
   using System.Web;
   using System.Web.UI;
   using System.Web.UI.WebControls;
   using System.Data;
   using System.Data.SqlClient;
   using System.Text;
   using WebApplication9.ServiceReference1;
   using System.Xml;
   using System.Runtime.Serialization.Json;
   using System.IO;
 
   namespace WebApplication9
   {
 
      public partial class _Default : System.Web.UI.Page
      {
 
         protected void Page_Init(object sender, EventArgs e)
         {
            WCFRSSNews();
         }
 
         protected void Page_Load(object sender, EventArgs e)
         {
 
         }
 
         private void WCFRSSNews()
         {
 
            Service1Client WCFObj = new Service1Client();
 
            WebApplication9.ServiceReference1.RSSGroup RSSGrp = WCFObj.ReturnNodeList();
 
            WCFObj.Close();
 
            DataSet ds = RSSGrp.RSSContent;
            DataTable dt = ds.Tables[0];
 
            Label1.Text = RSSGrp.Title;
 
            DropDownList1.DataTextField = "Title";
            DropDownList1.DataValueField = "Description";
 
            DropDownList1.DataSource = dt;
            DropDownList1.DataBind();
 
            TextBox1.Text = DropDownList1.SelectedValue.ToString();
 
         }
 
      }
 
   }