Use. Net to generate an RSS file so that the RSS reader can subscribe to it.

Source: Internet
Author: User
Tags mdb database

Getrss. CS class

Using system;
Using system. Collections. Generic;
Using system. Web;
Using system. Data;
Using system. configuration;

Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;

Using system. IO;

/// <Summary>
/// Summary of getrss
/// </Summary>
Public class getrss
{
/// <Summary>
/// Write the RSS File Based on the file path
/// </Summary>
/// <Remarks>
/// Example:
/// Writerss ("D: vs2005generaterssrss_folder est_tb.xml", "test_tb ")
/// Note: Adjust the generated RSS feed Based on the table structure. This instance is only for testing.
/// </Remarks>
/// <Param name = "pathfilename"> file path </param>
/// <Param name = "tablename"> table name </param>
/// <Returns> true or false </returns>
Public static bool writerss (string pathfilename, string tablename)
{
Try
{
Fileinfo finfo = new fileinfo (pathfilename );
Finfo. Delete (); // Delete the old one first

Using (filestream FS = finfo. openwrite ())
{
Streamwriter Sw = new streamwriter (FS, system. Text. encoding. getencoding ("UTF-8 "));
Sw. writeline (getrss. getrssstring (tablename ));
Sw. Flush ();
Sw. Close ();
}
Return true;
}
Catch (system. Exception ex)
{
System. Web. httpcontext. Current. response. Write (ex. Message );
Return false;
Throw;
}

}

/// <Summary>
/// Organize RSS strings that meet the latest standards
/// Parameter: Table name.
/// </Summary>
/// <Remarks>
/// Example:
/// Getrss ()
/// Note: Adjust the generated RSS feed Based on the table structure. This instance is only for testing.
/// </Remarks>
/// <Param name = "tablename"> table name </param>
/// <Returns> return a dataset data source </returns>
Public static string getrssstring (string tablename)
{
Try
{
Dataset DS = Common. runquery ("select * From tb_news"); // The common. runquery function is a user-defined query function.
String strrss = "";
Strrss = strrss + "<? XML version = \ "1.0 \"?> "+ System. environment. newline;
Strrss = strrss + "<RSS xmlns: xsi = \" http://www.w3.org/2001/XMLSchema-instance/ "" + system. environment. newline;
Strrss = strrss + "xmlns: XSD = \" http://www.w3.org/2001/XMLSchema/ "+ system. environment. newline;
Strrss = strrss + "xmlns: Slash = \" http://purl.org/rss/1.0/modules/slash// "version = \" 2.0 \ ">" + system. environment. newline;
Strrss = strrss + "<channel>" + system. environment. newline;
Strrss = strrss + "<title> subscription title </title>" + system. environment. newline;
Strrss = strrss + "<link> http://www.Hellocom.cn/</link>" + system. environment. newline;
Strrss = strrss + "<description> description </description>" + system. environment. newline;
Strrss = strrss + "<language> ZH-CN </language>" + system. environment. newline;
Strrss = strrss + "<generator> www.hellocom.cn </generator>" + system. environment. newline;
Strrss = strrss + "<copyright> Beijing Hello company </Copyright>" + system. environment. newline;
For (INT I = 0; I <Ds. Tables [0]. Rows. Count; I ++)
{
Strrss = strrss + "<item>" + system. environment. newline;
Strrss = strrss + "<title>" + Ds. Tables [0]. Rows [I] ["cname"] + "</title>" + system. environment. newline;
Strrss = strrss + "<link> http://www.Hello.com.cn/newsdetail/" + Ds. tables [0]. rows [I] ["CID"] + ". HTML </link> "+ system. environment. newline;
Strrss = strrss + "<author/>" + system. environment. newline;
Strrss = strrss + "<guid> http://www.Hello.com.cn/newsdetail/" + Ds. tables [0]. rows [I] ["CID"] + ". HTML </GUID> "+ system. environment. newline;
Strrss = strrss + "<pubdate>" + convert. todatetime (Ds. tables [0]. rows [I] ["ctime"]. tostring ()). tostring ("yyyy-mm-dd hh: mm") + "</pubdate>" + system. environment. newline;
Strrss = strrss + "<comments> http://www.Hello.com.cn/newsdetail/" + Ds. tables [0]. rows [I] ["CID"] + ". HTML </comments> "+ system. environment. newline;
Strrss = strrss + "<slash: Comments> 0 </slash: Comments>" + system. environment. newline;
Strrss = strrss + "<source url = \" http://www.Hello.com.cn/newsdetail/ "+ Ds. tables [0]. rows [I] ["CID"] + ". HTML \ ">" + Ds. tables [0]. rows [I] ["cname"] + "</source>" + system. environment. newline;
Strrss = strrss + "<description>" + Ds. Tables [0]. Rows [I] ["cname"] + "</description>" + system. environment. newline;
Strrss = strrss + "</item>" + system. environment. newline;
}
Strrss = strrss + "</channel>" + system. environment. newline;
Strrss = strrss + "</RSS>" + system. environment. newline;
Return strrss;
}
Catch (exception ex)
{
System. Web. httpcontext. Current. response. Write (ex. Message );
Throw;
}
}

}

========================================================== ==========================================

Default. aspx

<% @ Page Language = "C #" autoeventwireup = "true" codefile = "default. aspx. cs" inherits = "_ default" %>

<! 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">
<Head runat = "server">
<Title> </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>

</Div>
</Form>
</Body>
</Html>
========================================================== ==========================================

Default. aspx. CS

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. Web;
Using system. Web. UI;
Using system. Web. UI. webcontrols;

Public partial class _ default: system. web. UI. page
{< br> protected void page_load (Object sender, eventargs e)
{< br> string MSG = "generated successfully! ";
string tablename =" news "; // request. querystring ["tablename"];
string fname = system. web. httpcontext. current. server. mappath ("rss_folder") + "\" + tablename + ". XML ";
try
{< br> getrss. writerss (fname, tablename);
}< br> catch (system. exception E11)
{< br> MSG = "An error occurred while generating! ";
}

Common. msgbox (MSG );

}
}

========================================================== ==========================================

Create a folder: rss_folder

========================================================== ==========================================

Runquery functions in the common class

// Run the query
Public static dataset runquery (string querystring)
{

// Declare the connection string. This example uses oledb
// Connect to the # zn1_databak_en.mdb Database

Oledbconnection dbconnection = Common. getconn ();
Oledbdataadapter dbadapter;
Dataset resultsdataset = new dataset ();

Try
{

// Run the query and create a dataset
Dbadapter = new oledbdataadapter (querystring, dbconnection );
Dbadapter. Fill (resultsdataset );

// Close the database connection
Dbconnection. Close ();

}
Catch (exception ex)
{

// Close the database if the connection is still enabled
If (dbconnection. State = connectionstate. open)
{
Dbconnection. Close ();
}
// Msgbox ("unable to connect to the database! ");
// System. Web. httpcontext. Current. response. Write ("<SCRIPT> history. Back (1); </SCRIPT> ");
// System. Web. httpcontext. Current. response. Write (querystring + "<br/> ");
// System. Web. httpcontext. Current. response. Write (Ex );
// System. Web. httpcontext. Current. response. End ();

}

Return resultsdataset;

}

Http://hi.baidu.com/nirvanan/blog/item/a822e3cce2852d580fb3458b.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.