6年的blog,重新拾起,再来耕耘!
分类: WINDOWS
2007-07-24 14:50:32
主要是生成、读取xml文件,这里Repeater作为xml的载体,并不真正生成xml文件。
要注意语句,CDATA 节用于转义,否则将被识别为标记的文本块。类似html中的标签,用于兼容一些浏览器。
RssFeed.aspx
<%@ Page language="c#" Codebehind="RssFeed.aspx.cs" AutoEventWireup="false" Inherits="MyRss.RssFeed" %>
%# DataBinder.Eval(Container.DataItem, "newsid") %>
RssFeed.aspx.cs
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!this.IsPostBack)
{
Response.ContentType="text/xml";
SqlConnection con=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
SqlCommand cmd=new SqlCommand("select top 20 * from news order by newsid desc",con);
con.Open();
SqlDataReader dr=cmd.ExecuteReader();
Repeater1.DataSource=dr;
Repeater1.DataBind();
dr.Close();
con.Close();
}
}
protected string FormatForXML(object input)
{
string data=input.ToString();
data=data.Replace("&","&");
data=data.Replace("/",""");
data=data.Replace("'","&qapos;");
data=data.Replace("<","<");
data=data.Replace(">",">");
return data;
}
ReadRss.aspx
<%@ Page language="c#" Codebehind="ReadRss.aspx.cs" AutoEventWireup="false" Inherits="MyRss.ReadRss" %>
";
ProcessRSSItem(rssURL);%>
ReadRss.aspx.cs
public void ProcessRSSItem(string rssURL)
{
System.Net.WebRequest myRequest = System.Net.WebRequest.Create(rssURL);
System.Net.WebResponse myResponse = myRequest.GetResponse();
System.IO.Stream rssStream = myResponse.GetResponseStream();
System.Xml.XmlDocument rssDoc = new System.Xml.XmlDocument();
rssDoc.Load(rssStream);
System.Xml.XmlNodeList rssItems = rssDoc.SelectNodes("rss/channel/item");
string title = "";
string link = "";
string description = "";
for (int i = 0; i < rssItems.Count; i++)
{
System.Xml.XmlNode rssDetail;
rssDetail = rssItems.Item(i).SelectSingleNode("title");
if (rssDetail != null)
{
title = rssDetail.InnerText;
}
else
{
title = "";
}
rssDetail = rssItems.Item(i).SelectSingleNode("link");
if (rssDetail != null)
{
link = rssDetail.InnerText;
}
else
{
link = "";
}
rssDetail = rssItems.Item(i).SelectSingleNode("description");
if (rssDetail != null)
{
description = rssDetail.InnerText;
}
else
{
description = "";
}
Response.Write(" " + title + "
");
Response.Write(description + "
}
}