RSS是我们绝大多数网站都会使用的东西,它是一种信息聚合技术,能够非常高效和便利的发布和共享我们网站的数据,比如邮件订阅里面,RSS就体现出了高效性和便利性。
现在分享一个利用ASP.NET生成RSS文件的源代码,将DataTable里面的数据遍历出来,生成RSS。
/// /// 生成RSS /// /// 频道信息 /// RSS内容集合 public static void CreateRSS(RssChannel mRssChannel, List mRssItem) { XmlDocument mMainXmlDocument = new XmlDocument(); //编码 XmlDeclaration nodeDeclar = mMainXmlDocument.CreateXmlDeclaration("1.0", System.Text.Encoding.UTF8.BodyName, ""); mMainXmlDocument.AppendChild(nodeDeclar); XmlElement mRootXmlElement = mMainXmlDocument.CreateElement("rss"); //版本信息 mRootXmlElement.SetAttribute("version", "2.0"); mMainXmlDocument.AppendChild(mRootXmlElement); //Channel标签 XmlElement channelXmlElement = mMainXmlDocument.CreateElement("channel"); mRootXmlElement.AppendChild(channelXmlElement); XmlElement titleXmlElement = mMainXmlDocument.CreateElement("title"); XmlNode txtXmlNode = mMainXmlDocument.CreateTextNode(mRssChannel.Title); titleXmlElement.AppendChild(txtXmlNode); channelXmlElement.AppendChild(titleXmlElement); //Link标签 titleXmlElement = mMainXmlDocument.CreateElement("link"); txtXmlNode = mMainXmlDocument.CreateTextNode(mRssChannel.Link); titleXmlElement.AppendChild(txtXmlNode); channelXmlElement.AppendChild(titleXmlElement); //描述标签 titleXmlElement = mMainXmlDocument.CreateElement("description"); XmlNode cDataNode = mMainXmlDocument.CreateCDataSection(mRssChannel.Description); titleXmlElement.AppendChild(cDataNode); channelXmlElement.AppendChild(titleXmlElement); //创建ITEM子节点,里面的数据可以根据自己的需求添加或者减少。title和link标签是必须的,不能少。 foreach (RssItem child in mRssItem) { XmlElement itemNode = mMainXmlDocument.CreateElement("item"); titleXmlElement = mMainXmlDocument.CreateElement("title"); txtXmlNode = mMainXmlDocument.CreateTextNode(child.Title); titleXmlElement.AppendChild(txtXmlNode); itemNode.AppendChild(titleXmlElement); //链接 titleXmlElement = mMainXmlDocument.CreateElement("link"); txtXmlNode = mMainXmlDocument.CreateTextNode(child.Link); titleXmlElement.AppendChild(txtXmlNode); itemNode.AppendChild(titleXmlElement); //类型 titleXmlElement = mMainXmlDocument.CreateElement("category"); txtXmlNode = mMainXmlDocument.CreateTextNode(child.Category); titleXmlElement.AppendChild(txtXmlNode); itemNode.AppendChild(titleXmlElement); //描述 titleXmlElement = mMainXmlDocument.CreateElement("description"); txtXmlNode = mMainXmlDocument.CreateTextNode(child.Description); titleXmlElement.AppendChild(txtXmlNode); itemNode.AppendChild(titleXmlElement); //发布日期 titleXmlElement = mMainXmlDocument.CreateElement("pubDate"); txtXmlNode = mMainXmlDocument.CreateTextNode(child.PubDate); titleXmlElement.AppendChild(txtXmlNode); itemNode.AppendChild(titleXmlElement); //作者 titleXmlElement = mMainXmlDocument.CreateElement("author"); txtXmlNode = mMainXmlDocument.CreateTextNode(child.Author); titleXmlElement.AppendChild(txtXmlNode); itemNode.AppendChild(titleXmlElement); channelXmlElement.AppendChild(itemNode); } //保存 XmlTextWriter mXmlTextWriter = new XmlTextWriter(System.Web.HttpContext.Current.Server.MapPath("feed/rss.xml"), System.Text.Encoding.UTF8); mMainXmlDocument.WriteTo(mXmlTextWriter); mXmlTextWriter.Flush(); mXmlTextWriter.Close(); } /// /// RSS子数据 /// public class RssItem { private string title; public string Title { get { return title; } set { title = value; } } private string link; public string Link { get { return link; } set { link = value; } } private string description; public string Description { get { return description; } set { description = value; } } private string pubDate; public string PubDate { get { return pubDate; } set { pubDate = value; } } private string author; public string Author { get { return author; } set { author = value; } } private string category; public string Category { get { return category; } set { category = value; } } } /// /// RSS主数据 /// public class RssChannel { private string title; public string Title { get { return title; } set { title = value; } } private string link; public string Link { get { return link; } set { link = value; } } private string description; public string Description { get { return description; } set { description = value; } } }
使用的时候传入DataTable即可。
彭亚欧个人博客原创文章,转载请注明出处
文章关键词:C#动态生成RSS,ASP.NET生成RSS格式数据
文章固定链接:https://www.pengyaou.com/homeart/OQ==.html
上一篇 万网域名备案