【问题标题】:How to show Rss on my page如何在我的页面上显示 Rss
【发布时间】:2014-07-21 14:13:39
【问题描述】:

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

  • aspx:

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

  • aspx.cs:

    使用系统; 使用 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;

    公共部分类 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>

我做错了什么?

【问题讨论】:

  • 您在网络浏览器或 rss 客户端中收到了什么?
  • 提供的代码是否在一个 Web 请求中运行?
  • 我没有收到任何东西...
  • 请描述用户故事(工作流表单用户观点)用户应该如何请求 rss 页面以及它应该如何显示,应该是页面上的任何其他内容,除非 rss-xml

标签: c# asp.net xml xslt rss


【解决方案1】:

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

Msdn link to Respons.End description

您应该从代码 sn-p 中删除该行

【讨论】:

  • 当我删除该行时,它会显示所有连接到我的母版页的页面
  • 我听不懂你的回答,现在我迷失在你原来的问题中
  • 但是,如果有额外的不需要的内容,您是否会在结果中收到所需的 xml?
  • 我收到带有或不带有 Response.End() 的 xml。
  • 我需要删除以下行:Response.Clear(); Response.ContentType = "应用程序/rss+xml";响应。结束();谢谢大家
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-16
相关资源
最近更新 更多