这篇文章将为大家详细讲解有关如何通过XmlTextWriter实现生成RSS聚合页XML内容信息,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
通过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); } /// <summary> /// 取得聚合文章 /// </summary> /// <returns></returns> 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(); } }
关于“如何通过XmlTextWriter实现生成RSS聚合页XML内容信息”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。