07月04th

C#与RSS亲密接合

DIY编程技术1024℃我来说两句!
        动态生成RSS文件也基本有两种方法,一种是用字符串累加的方法,另一种是使用xml文档生成的方法。字符串累加的方法也比较简单,我也就不多说了,这里着重说一下生成XmlDocument的方法,包括各种节点的创建,属性的创建等。当然在此也有必要说明一下为什么采用后者,因为后者符合XMLDOM标准,有利于你认识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有样式表文件的话,加上这两句
     XmlProcessingInstructionnodeStylesheet =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部分是为说明实例手工添加):
<?xmlversion="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> 
</channel>
</rss> 
 
有几点值得说明的有:
1、 CreateTextNode,即创建文本结点
有人习惯使用InnerText来添加结点中的文本,虽然结果是一样的,但是要知道在DOM中文本也是结点,既然要符合DOM标准,就要进行到底!
2、 输出
我在实例中使用XmlTextWriter输出。
实际还可以使用如下:
Response.ContentType = "application/xml"; // 输出并按xml数据显示
Response.Write(domDoc.InnerXml);
但是,使用XmlTextWriter输出更快,所以也建议使用这个方法。

 

用XMLTextWriter方法实现如下:

XmlTextWriter writer = newXmlTextWriter(this.Response.OutputStream,System.Text.Encoding.UTF8);
writer.Formatting = Formatting.Indented;
writer.Indentation = 3;

writer.WriteStartDocument();

writer.WriteComment("Create usingXmlTextWriter at " + DateTime.Now);

writer.WriteStartElement("rss");
writer.WriteAttributeString("version","2.0");

writer.WriteStartElement("channel");
writer.WriteElementString("title","搜狐焦点新闻");
writer.WriteElementString("link","http://www.sohu.com");
writer.WriteCData("即时报道国内外时政大事,解读环球焦点事件");

//
//中间添加访问数据库部分...
//
writer.WriteEndElement();
writer.WriteEndElement();

writer.Flush();
writer.Close();

这个方法是把xml文件输出,如果要保存为xml文件,第一句用这样:
XmlTextWriter writer = newXmlTextWriter(Server.MapPath("grade.xml",null);  

本文出自:DIY博客园,链接:https://www.diybloghome.com/prology/397.html,转载请注明!