Chinaunix首页 | 论坛 | 博客
  • 博客访问: 183090
  • 博文数量: 27
  • 博客积分: 2510
  • 博客等级: 少校
  • 技术积分: 285
  • 用 户 组: 普通用户
  • 注册时间: 2007-03-20 10:21
个人简介

6年的blog,重新拾起,再来耕耘!

文章分类

全部博文(27)

文章存档

2013年(1)

2009年(9)

2007年(17)

我的朋友

分类: 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" %>




NewsShow

Rss Feed for 192.168.1.7



<%# FormatForXML(DataBinder.Eval(Container.DataItem,"newsname")) %>
]]>
%# DataBinder.Eval(Container.DataItem, "newsid") %>
<%# String.Format("{0:R}",DataBinder.Eval(Container.DataItem,"wtime")) %>







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" %>


 
  ReadRss
  
  
  
  
 
 
  


  <%
  string rssURL = "
";
  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 + "

");
   }
  }

阅读(1711) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~