Click here to check my latest exciting videos on youtube
Search Mallstuffs

Flag Counter
Spirituality, Knowledge and Entertainment


Locations of visitors to this page


Latest Articles


Move to top
Populating RSS XML Feed or XML data on Datagrid
Posted By Sarin on Jan 24, 2013     RSS Feeds     Latest Hinduism news
4936 Views

Introduction
Data is the backbone of any business application and one of the major issues in silver light is interacting with data source to retrieve data. For small line of business application, quite often the data is stored in xml. So, in this article, we will see how to retrieve data from xml data source and display it on silverlight data controls like datagrid. Additionally I will also show how to bind xml rss feed on silver light datagrid.
  
Populating Xml data on datagrid
In this example, my xml file will be in the client bin folder of web project. I have kept my xml small with the following data  
<NewsList>
  <News  Title="Russia checking 'dangerous' plane incident above Kabul"  Url="https://feedproxy.google.com/~r/NdtvNews-TopStories/~3/m3NjiQ4redk/story01.htm"/>
  <News  Title="Amanat' scored high in last exam, was friendly and popular: college"  Url="https://feedproxy.google.com/~r/NdtvNews-TopStories/~3/34mrFHTyx6c/story01.htm"/>
  <News  Title="Reliance Communications net profit falls 44%, shares plunge 9%"  Url="https://feedproxy.google.com/~r/NdtvNews-TopStories/~3/H-7Nc5JNo8s/story01.htm"/>
  <News  Title="Investors expect too much from India: Kamal Nath"  Url="https://feedproxy.google.com/~r/NdtvNews-TopStories/~3/fJ6kW5tytyM/story01.htm"/>
</NewsList>

Above xml file contains some of the news title and url from ndtv.com. I will populate this news item on click of a silver light button.  My Xaml and code behind will look like
  
Xaml
<Button Margin="220,20,0,0" HorizontalAlignment="Left" Grid.Row="1" Width="100" Height="30" Content="Read News Feed" Click="btnNews_Click"/>
<sdk:DataGrid Width="1200" x:Name="dgNews" Grid.Row="0" VerticalAlignment="Top" Height="200" HorizontalAlignment="Left" AutoGenerateColumns="True" />

Code behind
  private void btn_Click(object sender, RoutedEventArgs e)
         {
            WebClient webclient = new WebClient();
          webclient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(btn_DownloadStringCompleted);
            Uri Path = new Uri("News.xml", UriKind.Relative);
            webclient.DownloadStringAsync(Path);
        }
  void btn_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
         {
            if (e.Error == null)
             {
                XDocument doc = XDocument.Parse(e.Result);
                IEnumerable<News> list = from n in doc.Descendants("News")
                                         select new News
                                            {
                                               Title = (string)n.Attribute("Title"),
                                               Url = (string)n.Attribute("Url")
                                           };
                dgNews.ItemsSource = list;
            }
        }
   public class News
         {
            public string Title  get; set;
            public string Url  get; set;
        }
  
In the above code, I have defined a news class with two properties. Then, I have used this class to populate the news item on click a button. On button click, I have instantiated a web client to download the data in the xml file and then this string data is parsed to retrieve news items.  If the downloading of data encounters some error then the relevant error message is shown else the datagrid is populated with the news item. I have used the xml Ling query to parse the xml data. My populated datagrid will look like
  
Output


Populating XML News Feed
Next we will populate the news feed of ndtv.com. Url for ndtv.com news feed Is
<https://feeds.feedburner.com/NdtvNews-TopStories?format=xml>
I will just replace the local xml file path in the above code with this URL and the remaining code behind is  
void newsClient_DownloadStringCompleted(object sender,DownloadStringCompletedEventArgs e)
         {
            if (e.Error == null)
             {
                XmlReader reader = XmlReader.Create(new StringReader(e.Result));
                SyndicationFeed newsFeed = SyndicationFeed.Load(reader);
                reader.Close();
                IEnumerable<NewsFeed> newsList = from news in newsFeed.Items
                         select new NewsFeed
                           {
                           Title = news.Title.Text,
                           Summary = news.Summary.Text,
                           Url = news.Links.FirstOrDefault().Uri.ToString(),
                           PublishedDate = news.PublishDate.Date,
                          };
                dgNews.ItemsSource = newsList;
            }
            else
             {
                MessageBox.Show("Error: " + e.Error);
            }
        }
        public class NewsFeed
         {
            public DateTime PublishedDate  get; set;
            public string Title  get; set;
            public string Url  get; set;
            public string Summary  get; set;
        }

You need to add the System.ServiceModel.Syndication namespace to use the above code. In this piece of code, I have defined a NewsFeed class with four properties to store the news data retrieved from news feed.
  
On click of the button, I have used the xml reader class along with syndicationfeed class to parse the xml feed. In this syndicationfeed, I will get all the properties associated with the news item. These properties are populated onto the datagrid using NewsFeed Data class.
  
My populated datagrid look like:


Attached is the sample source code.
  
Conclusion
In this article, we will see how to download xml data, parse xml contents and bind it to silverlight datagrid.

Note: Images used on this website are either a production of Bhaktivedanta Book Trust(https://www.krishna.com), Iskcon Foundation or were found in google search under "Free to use and share". If any of the images presented here violates copyright issues or infringes anyone copyright or are not under "Fair use", then please bring it to our notice. Read Disclaimer for more.

Share this to your friends. One of your friend is waiting for your share.
Related Articles
Displaying Image in silverlight Datagrid
Visitors-Hits counter for your website
Why and how to create RSS feeds in C#
Simple Ajax client server Example
Ways of implementing AJAX
Data Validation using Ajax MaskedEditExtender control
Give rounded corners to your panel using Ajax
Validations using regular expressions and commonly used regex validations
Populating RSS XML Feed or XML data on Datagrid

Post Comment