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.Data.SqlClient;
public partial class Rss_Xml_RssNew : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
string xmlname = "RssNew.xml";
SqlDataReader dr = Rss_News.GetNoImgInfo(50);
DB2rss(xmlname, dr);
Jsp.Alert("Rss生成成功!");
}
catch
{
Jsp.Alert("不带图片的Rss生成失败!");
}
}
public void DB2rss(string XmlFile,SqlDataReader dr)
{
XmlTextWriter xr = new XmlTextWriter(Server.MapPath(XmlFile), System.Text.Encoding.GetEncoding("GB2312"));
xr.Formatting = Formatting.Indented;
xr.WriteStartDocument();
xr.WriteProcessingInstruction("xml-stylesheet", "type=\"text/css\" href=\"http://img.china.alibaba.com/news/rsstyle.css\"");
xr.WriteStartElement("rss");
xr.WriteAttributeString("version", "2.0");
xr.WriteStartElement("channel");
xr.WriteElementString("title", "新闻中心");//频道名称
xr.WriteElementString("link", "http://www.baidu.com/");//频道的URL
xr.WriteElementString("description", "新闻中心");//频道的描述
xr.WriteElementString("language", "zh-cn");//频道文章所用语言
xr.WriteElementString("managingEditor","[email protected]");//责任编辑的email
xr.WriteElementString("webMaster","[email protected]");//网站管理员email
xr.WriteElementString("generator","阿里巴巴提供");//该新闻的出处,一般填写合作网站
xr.WriteElementString("lastBuildDate", DateTime.Now.ToString("r"));//频道内容最后的修改日期
while (dr.Read())
{
xr.WriteStartElement("item");
xr.WriteElementString("title", dr.GetString(0));
xr.WriteElementString("author","☆小/ka喆¢");
xr.WriteElementString("link",Static_pages.getTimeRoute("http://www.baidu.com/news/news/",dr.GetDateTime(1).ToString()));
xr.WriteElementString("category","子类目id,这个id由alibaba提供 ");
xr.WriteElementString("tags", "新闻,资讯");
xr.WriteElementString("description", dr.GetString(2));
xr.WriteElementString("pubDate", dr.GetDateTime(3).ToString("r"));
xr.WriteEndElement();
}
//dr.Close();
xr.WriteEndElement();
xr.WriteEndElement();
xr.WriteEndDocument();
xr.Flush();
xr.Close();
}
protected void Button2_Click(object sender, EventArgs e)
{
try
{
string xmlname = "RssNewImg.xml";
SqlDataReader dr = Rss_News.GetImgInfo(50);
DB2rss(xmlname, dr);
Jsp.Alert("Rss生成成功!");
}
catch
{
Jsp.Alert("带图片的Rss生成失败!");
}
}
}
============================数据库操作
public static SqlDataReader GetNoImgInfo(int num)
{
string sql = "select top " + num + " biaoti,datetime,neirong,zjsxshijian from e_zixun where dalei <> '流行前沿' and renzheng=1 and CHARINDEX('<img',neirong) = 0 order by zjsxshijian desc";
SqlCommand cmd = new SqlCommand();
//定义对象资源保存的范围,一旦using范围结束,将释放对方所占的资源
using (SqlConnection conn = new SqlConnection(SqlHelper.ConnectionStringLocalTransaction))
{
//调用执行方法,因为没有参数,所以最后一项直接设置为null
//注意返回结果是dataset类型
SqlDataReader dr = SqlHelper.ExecuteReader(sql);
return dr;
}
}
public static SqlDataReader GetImgInfo(int num)
{
string sql = "select top " + num + " biaoti,datetime,neirong,zjsxshijian from e_zixun where dalei <> '流行前沿' and renzheng=1 and CHARINDEX('<img',neirong) > 0 order by zjsxshijian desc";
SqlCommand cmd = new SqlCommand();
//定义对象资源保存的范围,一旦using范围结束,将释放对方所占的资源
using (SqlConnection conn = new SqlConnection(SqlHelper.ConnectionStringLocalTransaction))
{
//调用执行方法,因为没有参数,所以最后一项直接设置为null
//注意返回结果是sqldatareader类型
SqlDataReader dr = SqlHelper.ExecuteReader(sql);
return dr;
}
}
=================================SqlHelper
public static SqlDataReader ExecuteReader(string strSQL)
{
SqlConnection connection = new SqlConnection(ConnectionStringLocalTransaction);
SqlCommand cmd = new SqlCommand(strSQL, connection);
try
{
connection.Open();
SqlDataReader myReader = cmd.ExecuteReader();
return myReader;
}
catch (System.Data.SqlClient.SqlException e)
{
throw new Exception(e.Message);
}
}