分享一个利用ASP.NET生成RSS文件的源代码,将DataTable里面的数据遍历出来,生成RSS。源代码:
[C#] 纯文本查看 复制代码 /// <summary>
/// 生成RSS
/// </summary>
/// <param name="mRssChannel">频道信息</param>
/// <param name="mRssItem">RSS内容集合</param>
public static void CreateRSS(RssChannel mRssChannel, List<RssItem> 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();
}
/// <summary>
/// RSS子数据
/// </summary>
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; }
}
}
/// <summary>
/// RSS主数据
/// </summary>
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; }
}
}
原文出处:http://www.pengyaou.com/Original/2014815/bbf69ec3-b4fb-4662-b200-2ceedf8fcb73.html
|