Advertisement
coasterka

News.aspx.cs

Apr 19th, 2015
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.62 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Xml;
  8. using System.Data;
  9.  
  10. namespace EBiznes1
  11. {
  12.     public partial class News : System.Web.UI.Page
  13.     {
  14.         XmlTextReader XmlTextRssReader;
  15.         XmlDocument XmlDocumentRss;
  16.         XmlNode XmlNodeRss;
  17.         XmlNode nodeChannel;
  18.         XmlNode nodeItem;
  19.         DataTable table = new DataTable();
  20.  
  21.         protected void Page_Load(object sender, EventArgs e)
  22.         {
  23.             DataColumn col = table.Columns.Add("Заглавие на новина");
  24.             DataColumn col1 = table.Columns.Add("Линк");
  25.             XmlTextRssReader = new XmlTextReader("http://rss.cnn.com/rss/edition_europe.rss");
  26.             XmlDocumentRss = new XmlDocument();
  27.             // load the xml content into object XmlDocument
  28.             XmlDocumentRss.Load(XmlTextRssReader);
  29.             // for loop to check the <rss> tag
  30.             for (int i = 0; i < XmlDocumentRss.ChildNodes.Count; i++)
  31.             {
  32.                 if (XmlDocumentRss.ChildNodes[i].Name == "rss")
  33.                     // if there is a <rss> tag, it's data is loaded
  34.                     XmlNodeRss = XmlDocumentRss.ChildNodes[i];
  35.             }
  36.             // check if there are tags in RSS do the following commands
  37.             if (XmlNodeRss.ChildNodes.Count != 0)
  38.             { // for loop to check the <channel> tag
  39.                 for (int i = 0; i < XmlNodeRss.ChildNodes.Count; i++)
  40.                 {
  41.                     if (XmlNodeRss.ChildNodes[i].Name == "channel")
  42.                         // if there is a <channel> tag
  43.                         nodeChannel = XmlNodeRss.ChildNodes[i];
  44.                 }
  45.                 // load channel's data in labels
  46.                 lblTitle.Text = "Title: " + nodeChannel["title"].InnerText;
  47.                 lblLanguage.Text = "Language: " + nodeChannel["language"].InnerText;
  48.                 lblLink.Text = "Link: " + nodeChannel["link"].InnerText;
  49.                 lblDescription.Text = "Description: " + nodeChannel["description"].InnerText;
  50.                 // for loop to fill in the titles and links to the news in GridView
  51.                 for (int i = 0; i < nodeChannel.ChildNodes.Count; i++)
  52.                 {
  53.                     if (nodeChannel.ChildNodes[i].Name == "item") // only check the news
  54.                     {
  55.                         nodeItem = nodeChannel.ChildNodes[i];
  56.                         // in the GridView: create a row that gets info about the news (title and link) from the tags
  57.                         DataRow row = table.NewRow();
  58.                         row[0] = nodeItem["title"].InnerText;
  59.                         row[1] = nodeItem["link"].InnerText;
  60.                         table.Rows.Add(row);
  61.                         table.AcceptChanges();
  62.                         GrViewNews.DataSource = table;
  63.                         GrViewNews.DataBind();
  64.                         //HyperLink hl = (HyperLink)GrViewNews.FindControl("HyperLink1");
  65.                         //hl.NavigateUrl = nodeItem["link"].InnerText;
  66.                         foreach (GridViewRow gvr in GrViewNews.Rows)
  67.                         {
  68.                             if (gvr.RowType == DataControlRowType.DataRow)
  69.                             {
  70.                                 HyperLink myHyperLink = gvr.FindControl("HyperLink1") as HyperLink;
  71.                                 myHyperLink.NavigateUrl = nodeItem["link"].InnerText;
  72.                             }
  73.                         }
  74.                     }
  75.                 }
  76.             }
  77.         }
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement