通过XmlTextWriter实现生成RSS聚合页XML内容信息

public partial class Rss2 : System.Web.UI.Page
{
string xmlDoc = "rss.xml";
protected void Page_Load(object sender, EventArgs e)
{
xmlDoc = Server.MapPath(xmlDoc);
GetRSS();
XmlDocument doc = new XmlDocument();
doc.Load(xmlDoc);
Response.ContentType = "text/xml";
doc.Save(Response.Output);
}
/// 
/// 取得聚合文章
/// 
/// 
public void GetRSS()
{
DataSet ds = new DataSet();
XmlTextWriter writer = new XmlTextWriter(xmlDoc, Encoding.UTF8);
writer.Formatting = Formatting.Indented;
writer.WriteStartDocument(true);
writer.WriteComment("RSS页的实现");
writer.WriteStartElement("rss");
writer.WriteAttributeString("version", "2.0");
writer.WriteStartElement("channel");
writer.WriteStartElement("title");
writer.WriteString("Danny_RSSDemo");
writer.WriteEndElement();
writer.WriteStartElement("link");
writer.WriteString("http://" + Request.ServerVariables["SERVER_NAME"]);
writer.WriteEndElement();
writer.WriteStartElement("description");
writer.WriteString("天道酬勤");
writer.WriteEndElement();
writer.WriteStartElement("copyright");
writer.WriteString("Copyright 2005");
writer.WriteEndElement();
writer.WriteStartElement("language");
writer.WriteString("zh-cn");
writer.WriteEndElement();
foreach (DataRow row in ds.Tables[0].Rows)
{
string NewsId = row["NewsId"].ToString();
string Heading = row["Heading"].ToString();
string Content = row["Content"].ToString();
string IssueDate = row["IssueDate"].ToString();
string ClassId = row["ClassId"].ToString();
writer.WriteStartElement("item");
writer.WriteStartElement("title");
writer.WriteString(Heading);
writer.WriteEndElement();
writer.WriteStartElement("link");
writer.WriteString("http://" + Request.ServerVariables["SERVER_NAME"] + "/NewsShow.aspx?ID=" + NewsId);
writer.WriteEndElement();
writer.WriteStartElement("description");
writer.WriteCData(Content);
writer.WriteEndElement();
writer.WriteStartElement("pubDate");
writer.WriteString(IssueDate);
writer.WriteEndElement();
writer.WriteStartElement("category");
writer.WriteString(ClassId);
writer.WriteEndElement();
writer.WriteEndElement();
}
writer.WriteEndElement();
writer.WriteEndElement();
writer.Flush();
writer.Close();
}
}