Mujtaba
Mujtaba

Reputation: 369

How to Save BBC RSS Feed with Images url into Database?

I'm creating a asp.net website for showing up rss feed from BBC, I'm doing exactly as i want that i m showing the rss feed on asp:DataList but my problem is that i dont want to show feed directly from a XML document, i want to store it firstly to my database and then show it to my website using Datalist. My older code is

<asp:DataList ID="DataList1" runat="server" DataSourceID="XmlDataSource1">
  <ItemTemplate>
<div class="jumbotron">
<h2><%# XPath("title") %></h2>
<br />
<h3><%# XPath("pubDate") %></h3>
<br />
<h3><%# XPath("description") %></h3>
<br />
<asp:Repeater runat="server" ID="_subitemsRepeater"
  EnableViewState="false"
  DataSource='<%# XPathSelect("media:thumbnail", XmlNamespaceManager) %>'>
  <ItemTemplate>
    <img src="<%# ((System.Xml.XmlNode)Container.DataItem).Attributes["url"].Value %>" />
    <br />
  </ItemTemplate>
  </asp:Repeater>
  <br />
  <a class="btn btn-primary btn-lg" target="_blank" href="<%# XPath("link") %>">Read More On This Story</a>
  </div>
  <hr />
</ItemTemplate>
</asp:DataList>
<asp:XmlDataSource ID="XmlDataSource1" runat="server"
DataFile="http://feeds.bbci.co.uk/news/education/rss.xml"
XPath="rss/channel/item" />

C# Code is in page load

public partial class _Default : Page
{
protected XmlNamespaceManager XmlNamespaceManager { get; set; }

protected void Page_Load(object sender, EventArgs e)
{
    XmlNamespaceManager =  new     XmlNamespaceManager(XmlDataSource1.GetXmlDocument().NameTable);
 XmlNamespaceManager.AddNamespace("media", "http://search.yahoo.com/mrss/");
}

}

and my new code is:

       FileWebRequest rssFeed = (FileWebRequest)WebRequest.Create(FeedURL);
       // DataSet rssData = new DataSet();
       //read the xml from the stream of the web request

        XDocument xd =     XDocument.Load(rssFeed.GetResponse().GetResponseStream());
        XNamespace media = "http://feeds.mashable.com/Mashable?format=xml";
        foreach (var feed in xd.Descendants("item"))
        {
            string title = feed.Element("title").Value.ToString();
            string description = feed.Element("description").Value.ToString();
            string link = feed.Element("link").Value.ToString();
            DateTime pubdate = DateTime.Parse(feed.Element("pubDate").Value);
            string thumb = (string)feed.Element(media + "thumbnail") != null ? (string)feed.Element(media + "thumbnail").Attribute("url").Value.ToString() : "file:///c:/No_Image.png";
            string connection = ConfigurationManager.ConnectionStrings["DefaultConnection"].ToString();
            SqlConnection NewCon = new SqlConnection(connection);
            NewCon.Open();
            SqlCommand NewCMD = new SqlCommand(TableName + "_Proc", NewCon);
            NewCMD.CommandType = CommandType.StoredProcedure;
            NewCMD.Parameters.AddWithValue("@title", title);
            NewCMD.Parameters.AddWithValue("@description", description);
            NewCMD.Parameters.AddWithValue("@link", link);
            NewCMD.Parameters.AddWithValue("@pubdate", pubdate);
            NewCMD.Parameters.AddWithValue("@thumb", thumb);
            NewCMD.ExecuteNonQuery();
            NewCon.Close();
        }   

in this code the problem is with images that it returns me the NoImage.jpg every time I'm connected to the internet. I think there is no problem with NameSpace too. I'm doing it because if the feed server or news server shutdown or not respond then my website not effected so. Anybody know how resolve this issue?. Any help will be greatly appreciated. thanks !

Upvotes: 0

Views: 510

Answers (0)

Related Questions