首页 新闻 会员 周边 捐助

aspx页面怎么输出XML

0
[已关闭问题]

        Response.ContentType = "text/xml";
        Response.Charset = "GB2312";
        Response.Write("<?xml version=\"1.0\" encoding=\"GB2312\"?>");

依然是按照html的方式输出的啊

aspx怎么输出成xml文件内容

poplau的主页 poplau | 初学一级 | 园豆:59
提问于:2008-11-11 20:27
< >
分享
其他回答(5)
0

        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();
}

5yplan | 园豆:1330 (小虾三级) | 2008-11-11 21:05
0

如果是想输出XML,建议使用.ashx文件而不使用.aspx,.ashx来做这件事要简单得多,而且效率也相对高一些

丁学 | 园豆:18730 (专家六级) | 2008-11-11 21:15
0

创建一个空白页面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());
  }

}

上不了岸的鱼 | 园豆:4613 (老鸟四级) | 2008-11-11 21:56
0

上不了岸的鱼{ttzhang}  的这句是关键

创建一个空白页面aspx文件中就只有下面一句:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Rss.aspx.cs" Inherits="FrontEnd_Rss" %>

小眼睛老鼠 | 园豆:2731 (老鸟四级) | 2008-11-12 11:52
0

你那只是在做字符串的组拼吗?

金鱼 | 园豆:1090 (小虾三级) | 2008-11-12 13:36
0

response.Clear();

Response.ContentType = "text/xml";

然后拼接xml格式的字符串(数据库端操作);

response.write(str);

注意删除.aspx页面中的内容,保留头部标记

chenlulouis | 园豆:215 (菜鸟二级) | 2008-11-21 16:09
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册