随笔 - 41,  文章 - 4,  评论 - 342,  阅读 - 10万
    一步一步来,从了解到制作,是一个连惯的过程。现在就让我们看 看RSS文件的一个简单的制作过程:

   private void  CreateRss()
    {
        XmlDocument xmlDoc 
= new XmlDocument();
        
//XML文件的说明
        XmlDeclaration xmlDec = xmlDoc.CreateXmlDeclaration("1.0", Encoding.UTF8.BodyName, "yes");
        xmlDoc.AppendChild(xmlDec);
//添加说明

        
//添加元素
        
//如果rss有样式表文件的话,加上这两句
        
// XmlProcessingInstruction xmlCss = xmlDoc.CreateProcessingInstruction("xml-stylesheet", "type=\"text/css\" href=\"Rss.css\"");
        
// xmlDoc.AppendChild(xmlCss);

        XmlElement root 
= xmlDoc.CreateElement("rss");
        root.SetAttribute(
"version""2.0");
        xmlDoc.AppendChild(root);

        
//添加频道
        XmlElement chNode = xmlDoc.CreateElement("channel");
        root.AppendChild(chNode);

        
//添加频道内容
        XmlElement element = xmlDoc.CreateElement("title");
        XmlNode textNode 
= xmlDoc.CreateTextNode("这是大标题");
        element.AppendChild(textNode);
        chNode.AppendChild(element);

        element 
= xmlDoc.CreateElement("link");
        textNode 
= xmlDoc.CreateTextNode("http://www.gkcity.com");
        element.AppendChild(textNode);
        chNode.AppendChild(element);

        element 
= xmlDoc.CreateElement("language");
        textNode 
= xmlDoc.CreateTextNode("zh-cn");
        element.AppendChild(textNode);
        chNode.AppendChild(element);

        element 
= xmlDoc.CreateElement("description");
        XmlNode node 
= xmlDoc.CreateCDataSection("这里是工控网的一些最新的相关信息?");
        element.AppendChild(node);
        chNode.AppendChild(element);
        
        
//从数据库中读取数据到RSS中去
        News[] rssItems = GetData();
        
foreach (News var in rssItems)
        {
            element 
= xmlDoc.CreateElement("item");
            XmlElement itemElement 
= xmlDoc.CreateElement("Title");
            XmlNode itemNode 
= xmlDoc.CreateTextNode(var.Title);
            itemElement.AppendChild(itemNode);
            element.AppendChild(itemElement);
            
//
            itemElement = xmlDoc.CreateElement("Link");
            itemNode 
= xmlDoc.CreateTextNode(var.Link);
            itemElement.AppendChild(itemNode);
            element.AppendChild(itemElement);
            
//
            itemElement = xmlDoc.CreateElement("PubDate");
            itemNode 
= xmlDoc.CreateTextNode(var.PubDate.ToString());
            itemElement.AppendChild(itemNode);
            element.AppendChild(itemElement);
            
//
            itemElement = xmlDoc.CreateElement("Description");
            itemNode 
= xmlDoc.CreateTextNode(var.Description);
            itemElement.AppendChild(itemNode);
            element.AppendChild(itemElement);

            chNode.AppendChild(element);
        }

        Response.ContentType 
= "application/xml";
        Response.Write(xmlDoc.InnerXml);
        //也可以用下面的来生成RSS文件。
        
//XmlTextWriter textWrite = new XmlTextWriter(this.Response.OutputStream, Encoding.UTF8);
        
//xmlDoc.WriteTo(textWrite);
        //textWrite.Flush();
        
//textWrite.Close();
    }

输出结果如下(item部分是为说明实例手工添加):

<?xml version="1.0" encoding="utf-8" ?>

<rss version="2.0">

<channel>

<title>搜狐焦点新闻</title>

<link>http://www.sohu.com</link>

<description>

<![CDATA[即时报道国内外时政大事,解读环球焦点事件

  ]]>

  </description>

<item id="">

        <title></title>

           <link></link>

           <pubDate>2006-10-15 21:59:36</pubDate>

  </item>

<item id="">

          <title></title>

           <link></link>

<pubDate>2006-10-15 10:33:53</pubDate>

</item>

  <title>[中介][出售住宅]明发国际新城32293万元/</title>

  <link>http://www.ewhouse.com/HouseInfo.aspx?publishId=3440</link>

  <pubDate>2006-10-12 10:50:18</pubDate>

  </item>

</channel>

</rss>

 

 

有几点值得说明的有:

1、  CreateTextNode,即创建文本结点

有人习惯使用InnerText来添加结点中的文本,虽然结果是一样的,但是要知道在DOM中文本也是结点,既然要符合DOM标准,就要进行到底!

2、  输出

我在实例中使用XmlTextWriter输出。

实际还可以使用如下:

Response.ContentType = "application/xml"; // 输出并按xml数据显示

Response.Write(domDoc.InnerXml);

但是,使用XmlTextWriter输出更快,所以也建议使用这个方法。


就这样一个简单的RSS文件生成了。注意在上面的News[] 数组是你自己定义的数据源,可以是从数据库读取过的,你有DataTable,DataSet也可以,只是你在把数据赋给结点的时候要弄清楚。

posted on   micenter  阅读(724)  评论(4编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示