RSS订阅已经能够使用。鉴于站不大,决定xml用动态的,不做静态生成。暂时rss相关的东西也没有做成单独的类,算是偷懒了。
基本思想就是用Response向页面输出一个二进制流。以下是我的基本代码,没有读数据库的部分:
XmlTextWriter xmltw = null;
xmltw = new XmlTextWriter(Response.OutputStream,Encoding.UTF8);
xmltw.Formatting = Formatting.Indented;
xmltw.Indentation = 3;
xmltw.WriteStartDocument();
xmltw.WriteStartElement("rss");
xmltw.WriteAttributeString("version", "2.0");
xmltw.WriteAttributeString("xmlns:content", "http://purl.org/rss/1.0/modules/content/");
xmltw.WriteAttributeString("xmlns:wfw", "http://wellformedweb.org/CommentAPI/");
xmltw.WriteAttributeString("xmlns:dc", "http://purl.org/dc/elements/1.1/");
xmltw.WriteAttributeString("xmlns:atom", "http://www.w3.org/2005/Atom");
xmltw.WriteAttributeString("xmlns:sy", "http://purl.org/rss/1.0/modules/syndication/");
//以上rss
//以下channel
xmltw.WriteStartElement("channel");
xmltw.WriteElementString("title", "阳光海岸");
xmltw.WriteElementString("link", "http://blog.cn-sun.net");
xmltw.WriteElementString("description", "Phoenix Sun's Blog");
xmltw.WriteElementString("pubDate", DateTime.Now.ToString("r"));
xmltw.WriteElementString("language", "en-us");
xmltw.WriteElementString("copyright", "Copyright 2010");
xmltw.WriteElementString("generator", "http://blog.cn-sun.net");
//以下item
xmltw.WriteStartElement("item");
xmltw.WriteElementString("title", "测试");
xmltw.WriteElementString("link", "/Topic.aspx?ID=1");
xmltw.WriteElementString("pubDate", "Mon, 08 Feb 2010 09:31:55 +0000");
xmltw.WriteStartElement("category");
xmltw.WriteCData("测试分类");
xmltw.WriteEndElement();
xmltw.WriteStartElement("guid");
xmltw.WriteAttributeString("isPermaLink", "false");
xmltw.WriteString("http://blog.cn-sun.net/Topic.aspx?ID=1");
xmltw.WriteEndElement();
xmltw.WriteStartElement("description");
xmltw.WriteCData("测试内容");
xmltw.WriteEndElement();
xmltw.WriteEndElement();
//以上item
xmltw.WriteEndElement();
xmltw.WriteEndDocument();
xmltw.Flush();
xmltw.Close();
Response.ContentType = "text/xml";
Response.ContentEncoding = Encoding.UTF8;
Response.End();
这是参照网上的方法简化的。网上那个方法看着太头大了=。=!