0

我正在创建一个用于显示来自 BBC 的 rss 提要的 asp.net 网站,我正在做的正是我希望在 asp:DataList 上显示 rss 提要但我的问题是我不想直接从 XML 显示提要文档,我想先将其存储到我的数据库中,然后使用 Datalist 将其显示到我的网站。我的旧代码是

<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# 代码在页面加载中

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/");
}

}

我的新代码是:

       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();
        }   

在这段代码中,问题在于每次我连接到互联网时它都会返回 NoImage.jpg 的图像。我认为 NameSpace 也没有问题。我这样做是因为如果提要服务器或新闻服务器关闭或没有响应,那么我的网站不会受到影响。有人知道如何解决这个问题吗?任何帮助将不胜感激。谢谢 !

4

0 回答 0