0
votes

I have created xml file using the code below(aspx.cs) and now i am trying to show the xml file on my page using xslt and literal control (look in my aspx)

  • aspx:

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

  • aspx.cs:

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.SqlClient; using System.Xml; using System.Text; using System.Configuration; using System.IO; using 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>

What am i doing wrong?

1
What are you receiving in web browser or rss client?Victor
Is the code provided running in the one web request?Victor
I am not recieving anything...user3082812
Please describe user story (workflow form user point of view) how the rss page should be requested by the user and how it should looks, should be anything else on the page unless rss-xmlVictor

1 Answers

0
votes

I think the error in misunderstanding of Response.End() call which "sends all currently buffered output to the client, stops execution of the page, and raises the EndRequest event."

Msdn link to Respons.End description

You should remove that line from your code snippet