asp.net 中的RSS的用法(三)--RSS制作

    一步一步来,从了解到制作,是一个连惯的过程。现在就让我们看 看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也可以,只是你在把数据赋给结点的时候要弄清楚。

转载于:https://www.cnblogs.com/seebook/articles/803521.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值