RSS 기사받아오기

WEF 웹서비스를 사용하기 위한 기본설정

      <service behaviorConfiguration="SLApp.Web.WCFServiceBehavior"
        name="SLApp.Web.WCFService">
        <!--
        <endpoint address="" binding="wsHttpBinding" contract="SLApp.Web.IWCFService">
          -->
        <endpoint address="" binding="basicHttpBinding" contract="SLApp.Web.IWCFService">

          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
  </system.serviceModel>
</configuration>

WCF 웹서비스

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Activation;
using System.Xml.Linq;
namespace SLApp.Web
{
    // NOTE: If you change the class name "WCFService" here, you must also update the reference to "WCFService" in Web.config.
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class WCFService : IWCFService
    {
        public void DoWork()
        {
        }

        public List<WCFDataType.RSS2_0> GetNewsRSS() {

            List<WCFDataType.RSS2_0> rss = new List<SLApp.Web.WCFDataType.RSS2_0>();
            XDocument rssFeed = XDocument.Load("http://news.kbs.co.kr/rss/kbsrss_news.php?site=news");
            var posts = from item in rssFeed.Descendants("item")
                        select new
                        {
                            Title = item.Element("title").Value,
                            Published = DateTime.Parse(item.Element("pubDate").Value),
                            Description = item.Element("description").Value,
                            Link = item.Element("link").Value,
                            Author = item.Element("author").Value
                        };

            foreach (var s in posts)
            {
                rss.Add(new SLApp.Web.WCFDataType.RSS2_0(s.Title, s.Author, s.Published, s.Description, s.Link));
            }

            return rss;
        }

------------------------------------------------------------------------------------------

//또다른 방법

      public  void Stock()
        {
            WebClient client = new WebClient();
            string s = client.DownloadString(new Uri("주식정보"));
            s = s.Trim();
            XDocument doc = XDocument.Parse(s);
            IEnumerable<XElement> current = doc.Element("StockInfo")
                .Element("channel")
                .Element("Stock_Sise")
                .Elements();
            Response.Write(current.ElementAt(0).Value);
        }

실제 받아오는 정보

<?xml version="1.0" encoding="euc-kr" ?>

- <StockInfo>

- <channel>

- <Stock_Sise>

<StockName>삼성전자</StockName>

<CurrentPoint>17,2500</CurrentPoint>

<UpDownPoint>50</UpDownPoint>

<UpDownFlag>2</UpDownFlag>

<UpDownSign></UpDownSign>

</Stock_Sise>

</channel>

</StockInfo>

------------------------------------------------------------------------------------------

    }
}

실버라이트에서 공유할 데이터

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Activation;
using System.Xml.Linq;

namespace SLApp.Web.WCFDataType
{
    [DataContract]
    public class RSS2_0
    {

        public RSS2_0(string p_Title, string p_Author, DateTime p_PubDate, string p_Description, string p_Link)
        {
            this.Title = p_Title;
            this.Author = p_Author;
            this.PubDate =p_PubDate;
            this.Description = p_Description;
            this.Link = p_Link;
        }
        private string _Title;
        private string _Description;
        private string _Author;
        private DateTime _PubDate;
        private string _Link;

        [DataMember]
        public string Title
        {
            get { return _Title; }
            set { _Title = value; }
        }

        [DataMember]
        public string Description
        {
            get { return _Description; }
            set { _Description = value; }
        }

        [DataMember]
        public string Author
        {
            get { return _Author; }
            set { _Author = value; }
        }

        [DataMember]
        public DateTime PubDate
        {
            get { return _PubDate; }
            set { _PubDate = value; }
        }

        [DataMember]
        public string Link
        {
            get { return _Link; }
            set { _Link = value; }
        }

    }
}

실버라이트

using System.Xml.Linq;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Collections.ObjectModel;
namespace PCGEP.SLApp.Contents
{
    public partial class NoticeNewsRSS : UserControl
    {
        public NoticeNewsRSS()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(NoticeNewsRSS_Loaded);
        }

        void NoticeNewsRSS_Loaded(object sender, RoutedEventArgs e)
        {

            Binding defaultBinding = new System.ServiceModel.BasicHttpBinding();
            EndpointAddress defaultAddress = new System.ServiceModel.EndpointAddress(@"http://localhost:9917/WCFService.svc");

            WCFServiceReference.WCFServiceClient  wc = new PCGEP.SLApp.WCFServiceReference.WCFServiceClient(defaultBinding, defaultAddress);
            wc.GetNewsRSSCompleted += new EventHandler<PCGEP.SLApp.WCFServiceReference.GetNewsRSSCompletedEventArgs>(wc_GetNewsRSSCompleted);
            wc.GetNewsRSSAsync();

        }

        void wc_GetNewsRSSCompleted(object sender, PCGEP.SLApp.WCFServiceReference.GetNewsRSSCompletedEventArgs e)
        {
            this.rssListBox.ItemsSource = e.Result;
        }
    }
}

+ Recent posts