Cs code
Private void Page_Load (object sender, System. EventArgs e)
{
SqlConnection cn = new SqlConnection ("server =.; database = company; uid = sa; pwd = sa ");
SqlDataAdapter da = new SqlDataAdapter ("select * from news", cn );
DataSet ds = new DataSet ();
Da. Fill (ds );
This. rptRss. DataSource = ds;
This. rptRss. DataBind ();
}
// Format the document into xml and replace some strings
Protected string FormatForXml (object input)
{
String data = input. ToString ();
Data = data. Replace ("&", "& amp ;");
Data = data. Replace ("/", "& quot ;");
Data = data. Replace ("'", "& qapos ;");
Data = data. Replace ("<", "& lt ;");
Data = data. Replace (">", "& gt ");
Return data;
}
Use Repeater to bind data. Note that the following aspx file format does not have any html code. Do not use it.
<% @ Page language = "c #" Codebehind = "myRssFeed. aspx. cs" ContentType = "text/xml" AutoEventWireup = "false" Inherits = "test. myRssFeed" %>
<Asp: Repeater id = "rptRss" runat = "server">
<HeaderTemplate>
& Lt; rss version = "2.0" & gt;
<Channel>
<Title> rss test </title>
<Link> www.sina.com.cn </link>
<Description> This is an rss test. </description>
</HeaderTemplate>
<ItemTemplate>
<Item>
<Title>
<% # FormatForXml (DataBinder. Eval (Container. DataItem, "title") %>
</Title>
<Description>
<! [CDATA [<% # FormatForXml (DataBinder. Eval (Container. DataItem, "content") %>]>
</Description>
<Link>
<% # "Http: // localhost/CompanySystem/shownewsdetails. aspx? NewsID = "+ FormatForXml (DataBinder. (Container. DataItem," newsID ") %>
</Link>
<Author> <% # FormatForXml (DataBinder. Eval (Container. DataItem, "author") %> </author>
<PubDate> <% # FormatForXml (DataBinder. Eval (Container. DataItem, "addTime") %> </pubDate>
<Category> <% # FormatForXml (DataBinder. Eval (Container. DataItem, "className") %> </category>
</Item>
</ItemTemplate>
<FooterTemplate>
</Channel> </rss>
</FooterTemplate>
</Asp: Repeater>
Example:
<? Xml version = "1.0" encoding = "gb2312"?>
<Xml>
<Asp: Repeater id = "r" runat = "server">
<Itemtemplate>
<Item id = "0" title = "ASP. net rss" url = "http: // 127.0.0.1/1.html" image = "http: // 127.0.0.1/1.jpg"/>
</Itemtemplate>
</Asp: Repeater>
</Xml>
Backend. cs:
Response. ContentType = "text/xml ";
Repeater. Data source...
Bind...