一步一步来,从了解到制作,是一个连惯的过程。现在就让我们看 看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 2007-07-02 20:02  micenter  阅读(718)  评论(4编辑  收藏  举报