.net 生成RSS xml

做rss我用到了 “周博通浏览器”
所以先下载一个


.net 生成RSS xml_第1张图片
image.png

是介个样子滴

点击右上角添加rss频道


.net 生成RSS xml_第2张图片
image.png

所以我们需要写个rss频道的地址
现在开始:
用的是Framework2.0的框架

创建一个 RssBuilder.cs 类

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Xml;
using System.Collections.Generic;
using System.IO;
///  
/// RssBuilder 的摘要说明
///  
public class RssBuilder
{
/// 
/// 生成RSS,写出文件
/// 
/// xml文件名称,格式xxx.xml
/// xml标题
/// xml描述
/// DataTable
/// 
public bool BuildRSS(string XmlName, string XmlTitle, string XmlDescription, DataTable dt)
{
    string LoadPath = System.Web.HttpContext.Current.Server.MapPath("~/rss/"); // RSS存放路径 
    if (!Directory.Exists(LoadPath))
    {
        Directory.CreateDirectory(LoadPath);
    }
    string domain = "";
    XmlTextWriter xtw = new XmlTextWriter(System.Web.HttpContext.Current.Server.MapPath("~/RSS/" + XmlName), System.Text.Encoding.GetEncoding("utf-8"));

    xtw.Formatting = Formatting.Indented;
    xtw.WriteStartDocument();
    //xtw.WriteProcessingInstruction("xml-stylesheet", "type='text/css' href='http://www.114390.com/images/font-awesome.min.css'"); // 增加头 
    xtw.WriteStartElement("rss"); // 增加rss节点 
    xtw.WriteAttributeString("version", "2.0");

    xtw.WriteStartElement("channel"); // 增加channel节点 
    xtw.WriteElementString("title", XmlTitle); // 频道名称 
    xtw.WriteElementString("link", domain); // 频道的URL 
    xtw.WriteElementString("description", XmlDescription); // 频道的描述
    xtw.WriteElementString("lastBuildDate ", DateTime.Now.ToString("r")); // 频道内容最后的修改日期

    // foreach (TableRow tr in ds.Tables[0].Rows) 
    for (int i = 0; i < dt.Rows.Count; i++)
    {
        xtw.WriteStartElement("item");
        //xtw.WriteAttributeString("id", (i + 1).ToString());
        xtw.WriteElementString("title", dt.Rows[i]["title"].ToString());
        xtw.WriteElementString("link", domain + "/info/" + dt.Rows[i]["ItemID"].ToString() + ".html"); // 链接地址 
        xtw.WriteElementString("description", dt.Rows[i]["Brief"].ToString());              // 描述 
        xtw.WriteElementString("pubDate", dt.Rows[i]["AddTime"].ToString());                        //发布时间
        xtw.WriteElementString("guid", dt.Rows[i]["NodeID"].ToString());                              //唯一标识
        xtw.WriteEndElement();
    }
    xtw.WriteEndElement();
    xtw.WriteEndElement();
    xtw.WriteEndDocument();
    xtw.Flush();
    xtw.Close();

    return true;
}
}

然后创建一个aspx的页面

在Page_Load方法里写

    string connnstr = "Data Source=192.168.0.10,1434;Initial Catalog=dhtx;User Id=sa;Pwd=*****";
    SqlConnection conn = new SqlConnection(connnstr);
    conn.Open();
    SqlDataAdapter sqlDa = new SqlDataAdapter(" select top 10 * from catalog_001_item ", conn);

    DataTable dtNews = new DataTable();
    sqlDa.Fill(dtNews);


    string fname = "rss.xml";
    string xmltitle = "新闻";
    string xmlDescription = "新闻";
    RssBuilder rssbuild = new RssBuilder();
    if (rssbuild.BuildRSS(fname, xmltitle, xmlDescription, dtNews))
    {
        Response.Redirect("rss/rss.xml");
    }

然后将我们这个网址粘贴到周博通浏览器的新增rss频道哪里就ok了

你可能感兴趣的:(.net 生成RSS xml)