网站的RSS一般以两种形式引用。一种是已经存在的xml文件,然后在更新数据库的时候对其进行更新,或者使用其它维护程序为其更新。另一种是在动态生成RSS文件,即在访问某一个地址的时候,服务端方法从数据库读取最新记录,生成RSS文件,返回给访问者。

 

现讲述动态生成RSS文件的方法。

动态生成RSS文件也基本有两种方法,一种是用字符串累加的方法,另一种是使用xml文档生成的方法。字符串累加的方法也比较简单,我也就不多说了,这里着重说一下生成XmlDocument的方法,包括各种节点的创建,属性的创建等。当然在此也有必要说明一下为什么采用后者,因为后者符合XML DOM标准,有利于你认识dom模型,并且构造速度更快,构造出的xml文档更不容易出错,其中有一些细节我也会做一些必要的讲述。

 

主方法如下:

private void WriteRSS()

{

     XmlDocument domDoc = new XmlDocument();

     XmlDeclaration nodeDeclar = domDoc.CreateXmlDeclaration("1.0", System.Text.Encoding.UTF8.BodyName, "yes");

     domDoc.AppendChild(nodeDeclar);

 

     //如果rss有样式表文件的话,加上这两句

     XmlProcessingInstruction nodeStylesheet = domDoc.CreateProcessingInstruction("xml-stylesheet","type=\"text/css\" href=\"rss.css\"");

     domDoc.AppendChild(nodeStylesheet);

 

     XmlElement root = domDoc.CreateElement("rss");

     root.SetAttribute("version","2.0");  //添加属性结点

     domDoc.AppendChild(root);

 

     XmlElement chnode = domDoc.CreateElement("channel");

     root.AppendChild(chnode);

 

     XmlElement element = domDoc.CreateElement("title");

     XmlNode textNode = domDoc.CreateTextNode("搜狐焦点新闻");    //文本结点

     element.AppendChild(textNode);

     chnode.AppendChild(element);

 

     element = domDoc.CreateElement("link");

     textNode = domDoc.CreateTextNode("http://www.sohu.com");

     element.AppendChild(textNode);

     chnode.AppendChild(element);

 

     element = domDoc.CreateElement("description"); //引用结点

     XmlNode cDataNode = domDoc.CreateCDataSection("即时报道国内外时政大事,解读环球焦点事件");

     element.AppendChild(cDataNode);

     chnode.AppendChild(element);

 

     DataTable dt = GetDataTab();     //访问数据库,获取要在rss中显示的记录

 

     foreach(DataRow dr in dt.Rows)

     {

         element = domDoc.CreateElement("item");

 

         //...

         //创建内容结点,常见的如title,description,link,pubDate,创建方法同上

         //...

 

         chnode.AppendChild(element);

     }

 

     //输出

     XmlTextWriter objTextWrite = new XmlTextWriter(this.Response.OutputStream,System.Text.Encoding.UTF8);

     domDoc.WriteTo(objTextWrite);

     objTextWrite.Flush();

     objTextWrite.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输出更快,所以也建议使用这个方法。

posted on 2006-10-16 14:08  不做懒人  阅读(3928)  评论(12编辑  收藏  举报