WCF Silverlight RSSreader

Some Intro:

In this post I want to explain the application I build for reading RSS feeds by using WCF Services and Silverlight. I can’t get used to write blog posts yet but I hope so. As a note if you cannot understand the code feel free to comment or ask for details.

Output will be like this:

untitled

Let’s get started:

First create a WCF Service  and in IService1.cs create our contracts.

using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace WcfService1
{
    // NOTE: If you change the interface name "IService1" here, you must also update the reference to "IService1" in Web.config.
    [ServiceContract]
    public interface IService1
    {

        //string GetXML(string Adress);
        [OperationContract]
        List<Customer> GetXML(string Adress);

        // TODO: Add your service operations here
    }
    [DataContract]
    public class Customer
    {
        [DataMember]
        public string Title { get; set; }
        [DataMember]
        public string Link { get; set; }
        [DataMember]
        public string Description { get; set; }
    }

}

Then in Service1.svc.cs implement our service.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Xml;
using System.IO;
using System.Web.UI.WebControls;

namespace WcfService1
{
 // NOTE: If you change the class name "Service1" here, you must also update the reference to "Service1" in Web.config and in the associated .svc file.
 public class Service1 : IService1
 {
 XmlTextReader rssReader;
 XmlDocument rssDoc;
 XmlNode nodeRss;
 XmlNode nodeChannel;
 XmlNode nodeItem;

 public List<Customer> GetXML(string Adress)
 //public string GetXML(string Adress)
 {
 List<Customer> result = new List<Customer>();

 rssReader = new XmlTextReader(Adress);
 rssDoc = new XmlDocument();

 rssDoc.Load(rssReader);

 for (int i = 0; i < rssDoc.ChildNodes.Count; i++)
 {

 if (rssDoc.ChildNodes[i].Name == "rss")
 {

 nodeRss = rssDoc.ChildNodes[i];
 }
 }

 for (int i = 0; i < nodeRss.ChildNodes.Count; i++)
 {

 if (nodeRss.ChildNodes[i].Name == "channel")
 {

 nodeChannel = nodeRss.ChildNodes[i];
 }
 }

 for (int i = 0; i < nodeChannel.ChildNodes.Count; i++)
 {

 if (nodeChannel.ChildNodes[i].Name == "item")
 {
 nodeItem = nodeChannel.ChildNodes[i];

 //result = "Title:\n" + nodeItem["title"].InnerText.ToString() + "\nPublication Date:\n" + nodeItem["pubDate"].InnerText.ToString() + "\nLink:\n" + nodeItem["link"].InnerText.ToString();

 result.Add(new Customer() { Title = nodeItem["title"].InnerText.ToString(), Link = nodeItem["link"].InnerText.ToString() });
 //string[] names = new string[i];

 }

 }

 return result;

 }
 }
}

And after “Adding WCF Service as Reference either manually or by use of Visual Studio right click tool” in Silverlight Application change Page.xaml.cs as follows:

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 SilverlightApplication1.ServiceReference1;

namespace SilverlightApplication1
{
    public partial class Page : UserControl
    {
        public Page()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Service1Client x = new Service1Client();
            x.GetXMLCompleted += new EventHandler<GetXMLCompletedEventArgs>(x_GetXMLCompleted);
            x.GetXMLAsync(RSSkaynak.Text);
        }

        void x_GetXMLCompleted(object sender, GetXMLCompletedEventArgs e)
        {

            for (int i = 0; i <= 10; i++)
            {
                HyperlinkButton x = new HyperlinkButton();
                x.NavigateUri = (new Uri(e.Result[i].Link.ToString()));
                x.Content = "Go!";
                x.TargetName = "_blank";
                liste.Items.Add("Title:\n" + e.Result[i].Title.ToString() + "\nLink:\n" + e.Result[i].Link.ToString());
                liste.Items.Add(x);

            }
        }
    }
}

Download Sample Solution

Thanks for your patience!
kick it on DotNetKicks.com

About these ads