private void ResponseXML(System.Text.StringBuilder xmlnode)
{
System.Web.HttpContext.Current.Response.Clear();
System.Web.HttpContext.Current.Response.ContentType = "Text/XML";
System.Web.HttpContext.Current.Response.Expires = 0;
System.Web.HttpContext.Current.Response.Cache.SetNoStore();
System.Web.HttpContext.Current.Response.Write(xmlnode.ToString());
System.Web.HttpContext.Current.Response.End();
}
如果是想输出XML,建议使用.ashx文件而不使用.aspx,.ashx来做这件事要简单得多,而且效率也相对高一些
创建一个空白页面aspx文件中就只有下面一句:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Rss.aspx.cs" Inherits="FrontEnd_Rss" %>
下面是后置代码文件的内容,输出XML【RSS】的页面的部分代码,希望对你有所帮助:
public partial class FrontEnd_Rss : System.Web.UI.Page
{
const int RSS_ITEMS = 50;
private int topicId = 0;
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
Common.TryGetIntFromRequest("id", out topicId, this.Request);
CreateRssFeed(topicId);
}
}
public void CreateRssFeed(int id)
{
// Set the content-type
Response.Clear();
Response.ContentType = "text/xml";
Response.ContentEncoding = Encoding.UTF8;
// check to see if a cached version exists
//if exists ,then remove it,just for testing
//if (Cache["RssFeed"] != null)
//{
// Cache.Remove("RssFeed");
//}
//if (Cache["RssFeed"] == null)
//{
// build up the cache dynamically
//取数据
DataTable articleData = GetDataSource(RSS_ITEMS, id);
// Use an XmlTextWriter to write the XML data to a string...
StringWriter sw = new StringWriter();
XmlTextWriter writer = new XmlTextWriter(sw);
// write out <rss version="2.0">
writer.WriteStartElement("rss");
writer.WriteAttributeString("version", "2.0");
// write out <channel>
writer.WriteStartElement("channel");
// write out <channel>-level elements
writer.WriteElementString("title", "ISACA最新文章列表!");
writer.WriteElementString("link", Common.GetBasicConfigDomainName());
writer.WriteElementString("docs", Common.GetBasicConfigDomainName() + "/FrontEnd/Rss.aspx");
writer.WriteElementString("language", "zh-cn");
writer.WriteElementString("copyright", "Copyright @2008,ISACA All Rights Reservered");
writer.WriteElementString("lastBuildDate", DateTime.Now.ToString("yyyy年MM月dd日"));
writer.WriteElementString("description", "欢迎订阅ISACA最新文章列表.");
writer.WriteElementString("ttl", "60");
// write out an <item> element for each of the first X articles
for (int i = 0; i < articleData.Rows.Count; i++)
{
// write out <item>
writer.WriteStartElement("item");
// write out <item>-level information
writer.WriteElementString("title", articleData.Rows[i]["Title"].ToString());
writer.WriteElementString("link", String.Format("{0}{1}{2}", Common.GetBasicConfigDomainName(), articleData.Rows[i]["FilePath"], articleData.Rows[i]["FileName"]));
//Other1字段存储摘要信息
writer.WriteStartElement("description");
string descript = string.Empty;
//Format函数格式化字符串
descript += string.Format("文章标题:{0}<br />", articleData.Rows[i]["Title"]);
descript += string.Format("文章类别:{0}<br />", articleData.Rows[i]["TopicName"]);
descript += string.Format("作者:{0}<br />", Format(articleData.Rows[i]["Author"].ToString()));
descript += string.Format("发布时间:{0}<br />", articleData.Rows[i]["PublishDate"]);
descript += string.Format("浏览次数:{0}<br />", articleData.Rows[i]["ViewCount"]);
descript += string.Format("关键字:{0}<br />", Format(articleData.Rows[i]["Keywords"]));
descript += string.Format("内容摘要:<br />{0}", Format(articleData.Rows[i]["Other1"]));
writer.WriteCData(descript);
writer.WriteEndElement();
//writer.WriteElementString("description", string.Format("<![CDATA[{0}]]", articleData.Rows[i]["Other1"].ToString()));
writer.WriteElementString("category", Format(articleData.Rows[i]["TopicName"].ToString()));
writer.WriteElementString("author", Format(articleData.Rows[i]["Author"].ToString()));//author's email
// use DateTimeFormatInfo "r" to use RFC 1123 date formatting (same as RFC 822)
writer.WriteElementString("pubDate", ((DateTime)articleData.Rows[i]["PublishDate"]).ToString("r"));
writer.WriteElementString("source", "ISACA最新文章列表");
//<enclosure url="http://www.isaca.org/image.aspx?imageID=153" type="image/jpeg"></enclosure>
// write out </item>
writer.WriteEndElement();
}
// write out </channel>
writer.WriteEndElement();
// write out </rss>
writer.WriteEndElement();
// save the string in the cache (cache for 2.0 hours)
//Cache.Insert("RssFeed", sw.ToString(), null, DateTime.Now.AddHours(2.0), TimeSpan.Zero);
writer.Close();
//}
// write out the cached value
//Response.Write(Cache["RssFeed"].ToString());
Response.Write(sw.ToString());
}
}
上不了岸的鱼{ttzhang} 的这句是关键
创建一个空白页面aspx文件中就只有下面一句:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Rss.aspx.cs" Inherits="FrontEnd_Rss" %>
你那只是在做字符串的组拼吗?
response.Clear();
Response.ContentType = "text/xml";
然后拼接xml格式的字符串(数据库端操作);
response.write(str);
注意删除.aspx页面中的内容,保留头部标记