ASP.NET创建RSS提要,没有错误但没有数据

时间:2012-05-16 14:57:36

标签: c# asp.net .net rss

好的记录,我比任何.NET更熟悉Java和RnR。我在这里和那里修复了一个函数中的一些错误,并在这里和那里搜索了一些新的“功能”,但这不是我的专业知识。好吧,请注意,这是我的问题,我需要为我们在ASP.NET中构建的站点创建一个RSS源。谷歌提出了一些解决方案,但我用这个作为例子: http://www.aspfree.com/c/a/C-Sharp/Creating-an-RSS-Feed-with-ASP-Net-Written-in-C-Sharp/

奇怪的是,我把它全部用到没有我能看到的错误,但是当我运行它时我只得到一个空白页面。我已经测试了SQL,它可以工作并返回我想要的行,但我不知道如何诊断它是否连接到数据库或为什么它没有返回任何东西。这是代码:

asp.aspx.cs:

using System;
using System.Text;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Xml;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Diagnostics;


       //Class for returning RSS feed Data

   public partial class rss : System.Web.UI.Page
{

    private void Page_Load(object sender, System.EventArgs e)
    {
        string sSource;
        string sLog;
        string sEvent;
        sSource = "RSS Feed";
        sLog = "Application";
        sEvent = "Database Connectiont";
       // Connect to the Database
       SqlConnection myConnection = new SqlConnection("Data Source=localhost;Initial Catalog=events;Persist Security Info=True;User ID=wa;Password=password;");
       // Retrieve the SQL query results and bind it to the Repeater
       const string SQL_QUERY = "SELECT EventName, Comments, URL, StartDate FROM event WHERE SaleDateTo > getdate() AND SaleDateFrom < getdate() AND redirecttourl is not null ORDER BY EventDate";
       SqlCommand myCommand = new SqlCommand(SQL_QUERY, myConnection);
       bool IsDbAvailable = true;
       try {
           myConnection.Open();
           rptRSS.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
           rptRSS.DataBind();
           myConnection.Close();
           }
           catch {
                IsDbAvailable = false;
                if (!EventLog.SourceExists(sSource))
                        EventLog.CreateEventSource(sSource,sLog);
            }
        finally {
            myConnection.Close();
        }     
    }

    protected string FormatForXML(object input)
    {
       string data = input.ToString(); //cast input to string

       // replace those characters disallowed in XML documents
       data = data.Replace("&", "&amp;");
       data = data.Replace("\"", "&quot;");
       data = data.Replace("'", "&apos;");
       data = data.Replace("<", "&lt;");
       data = data.Replace(">", "&gt;");

       return data;
    }
}

rss.aspx:

    <%@ Page language="c#" CodeFile="rss.aspx.cs" ContentType="text/xml" AutoEventWireup="false" Inherits="rss"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<asp:Repeater id="rptRSS" runat="server">

  <HeaderTemplate>
    <rss version="2.0">
      <channel>
        <title>TA On-Sale Feed</title>
        <link>http://www.website.com/rss.aspx</link>
        <description>All events current on-sale</description>
  </HeaderTemplate>

    <itemTemplate>
    <event>
    <title><%#FormatForXML(DataBinder.Eval(Container.DataItem,"EventName"))%>
    </title>
    <description>
    <%#FormatForXML(DataBinder.Eval(Container.DataItem, "Comments"))%>
    </description>
    <link>http://order.ticketalternative.com<%#DataBinder.Eval(Container.DataItem, "url")%>
    </link>
    <eventdate>
    <%#DataBinder.Eval(Container.DataItem, "StartDate")%>
    <eventdate>
    </event>
    </ItemTemplate>
     <FooterTemplate>
          </channel>
        </rss>
      </FooterTemplate>
    </asp:Repeater>
</html>

1 个答案:

答案 0 :(得分:0)

使用SQLDataAdpter将结果集填充到DataTable / Dataset中,然后将数据集/表分配给rtpRSS.DataSource。