有时asp.net文件输出纯XML数据很有用,这时的asp.net文件就相当于一个WebService,输出XML文档也很简单,分以下几部
1、设置输出类型-Response.ContentType = "text/xml";
2、设置字符集-Response.Charset = "GB2312";
3、清空输入内容-Response.Clear();
4、构造XML内容并输出-Response.Write(...)
最后结束余下的输出Response.End(),

DataSet ds = new DataSet();
        DataTable table 
= new DataTable("Item");
        table.Columns.Add(
"Name"typeof(string));
        table.Columns.Add(
"Price"typeof(string));

        DataRow r 
= table.NewRow();
        r[
0= "huzhen";
        r[
1= "huzhen";
        table.Rows.Add(r);

        r 
= table.NewRow();
        r[
0= "huzhen";
        r[
1= "huzhen";
        table.Rows.Add(r);

        ds.Tables.Add(table);
        Response.ContentType 
= "text/xml";
        Response.Charset 
= "GB2312";
        Response.Clear();
        Response.Write(
"<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n " + ds.GetXml());
        Response.End();
posted on 2009-03-17 23:43  钱途无梁  阅读(686)  评论(0编辑  收藏  举报