如何在我的页面上显示Rss

时间:2014-06-01 08:17:14

标签: c# asp.net xml xslt rss

我使用下面的代码(aspx.cs)创建了xml文件,现在我尝试使用xslt和literal控件在我的页面上显示xml文件(查看我的aspx)

  • ASPX:

                    <asp:Literal ID="RssHtml" runat="server" />
    

  • aspx.cs:

    使用System; 使用System.Collections.Generic; 使用System.Linq; 使用System.Web; 使用System.Web.UI; 使用System.Web.UI.WebControls; 使用System.Data.SqlClient; 使用System.Xml; 使用System.Text; 使用System.Configuration; 使用System.IO; 使用System.Xml.Xsl;

    public partial class Rss:System.Web.UI.Page {     protected void Page_Load(object sender,EventArgs e)     {

    Response.Clear();
    Response.ContentType = "application/rss+xml";
    XmlTextWriter objX = new XmlTextWriter(Server.MapPath("App_Code/RssDef.xml"), Encoding.UTF8);
    objX.WriteStartDocument();
    objX.WriteStartElement("rss");
    objX.WriteAttributeString("version", "2.0");
    objX.WriteStartElement("channel");
    
    SqlCommand cmd = new SqlCommand("Select * from RssFeeds", new SqlConnection(ConfigurationManager.ConnectionStrings["igroup13_test1ConnectionString"].ConnectionString));
    cmd.Connection.Open();
    SqlDataReader dr = cmd.ExecuteReader();
    
    objX.WriteElementString("title", "RSS.....");
    objX.WriteElementString("link", "");
    objX.WriteElementString("description", "desc");
    objX.WriteElementString("language", "en-us");
    objX.WriteElementString("ttl", "60");
    objX.WriteElementString("lastBuildDate", String.Format("{0:R}", DateTime.Now));
    
    while (dr.Read())
    {
        objX.WriteStartElement("item");
        objX.WriteElementString("title", dr["title"].ToString());
        objX.WriteElementString("link", "");
        objX.WriteElementString("description", dr["description"].ToString());
        objX.WriteElementString("pubDate", String.Format("{0:R}", dr["publishDate"]));
        objX.WriteEndElement();
        //objX.WriteEndElement();
    }
    
    objX.WriteEndElement();
    objX.WriteEndElement();
    objX.WriteEndDocument();
    objX.Flush();
    objX.Close();
    Response.End();
    
    
    #region load the XML file
    // Use my local XML file (that I've created)
    String strXmlSrc = Server.MapPath("~/App_Code/RssDef.xml");
    
    //  Load the XML file into the XmlDocument object.
    XmlDocument myXmlDoc = new XmlDocument();
    try
    {
        myXmlDoc.Load(strXmlSrc);
    }
    catch (Exception ex)
    {
        Response.Write("error in loading XML document " + ex.Message);
        return;
    }
    #endregion
    
    #region load the XSLT file
    //  Load our XSL file into the Xsl Transform object.
    String strXslFile = Server.MapPath("~/App_Data/Def.xslt");
    XslCompiledTransform myXslDoc = new XslCompiledTransform(true);
    try
    {
        myXslDoc.Load(strXslFile);
    }
    catch (Exception ex)
    {
        Response.Write("error in loading XSLT document " + ex.Message);
        return;
    }
    #endregion
    
    #region Transform the XML into XHTML
    //  Create a StringBuilder and then point a StringWriter at it.
    //  I'm using this to hold the HTML output by the Transform method
    StringBuilder myStringBuilder = new StringBuilder();
    StringWriter myStringWriter = new StringWriter(myStringBuilder);
    
    try
    {
        myXslDoc.Transform(myXmlDoc, null, myStringWriter);
    }
    catch (Exception ex)
    {
        Response.Write("error in transforming the document " + ex.Message);
        return;
    }
    #endregion
    
    #region Write to the HTML Page
    //  Take theresulting HTML and display it via an ASP.NET
    //  literal control.
    RssHtml.Text = myStringBuilder.ToString();
    #endregion
    
    
    }
    

    }

  • XSLT:

  

<xsl:for-each select="rss/channel">
  <h2>
    <a href="{link}">
      <xsl:value-of select="title" />        
    </a>

  </h2>
  <h4>
    <xsl:value-of select="description"/>
  </h4>
</xsl:for-each>

<ul>
  <xsl:for-each select="rss/channel/item">
    <li>
      <a href="{link}">
        <strong>
          <xsl:value-of select="title" />

        </strong>
      </a>

    </li>

    <xsl:value-of select="descreption"/>
    <br/>
    <xsl:value-of select="pubDate"/>

  </xsl:for-each>
</ul>

我做错了什么?

1 个答案:

答案 0 :(得分:0)

我认为误解Response.End()的错误调用“将所有当前缓冲的输出发送到客户端,停止执行页面,并引发EndRequest事件。”

Msdn link to Respons.End description

您应该从代码段中删除该行