小光网络技术博客

介绍和研究搜索引擎优化、SEO、asp、asp.net、php、jsp技术

« ashx 里面如何给Session赋值ASP.NET2.0 上传图片并为用户创建相应文件夹 »

asp.net输出纯xml格式数据

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Server01.aspx.cs" Inherits="Server01" %>
只保留以上这行代码。

.cs文件主要是:
public partial class Server01 : System.Web.UI.Page 

private string connString = @"server=BJ2-JIANGFJFDBSERVER;Trusted_Connection=false;database=TestDB;uid=sa;pwd=7929009"


protected void Page_Load(object sender, EventArgs e) 

Response.Clear(); 
Response.ContentType 
= "text/xml"
Response.Charset 
= "UTF-8"

getDataset(); 
// getXml(); 

Response.End(); 
}
 

private void getDataset() 

DataSet ds 
= new DataSet(); 
SqlConnection conn 
= new SqlConnection(connString); 

SqlCommand cmd 
= new SqlCommand("select * from TA",conn); 

SqlDataAdapter sda 
= new SqlDataAdapter(cmd); 

sda.Fill(ds); 
DataTable dt 
= ds.Tables[0]; 

StringBuilder xmlData 
= new StringBuilder(); 
xmlData.AppendLine(
@"<?xml version='1.0' encoding='UTF-8' ?>"); 
xmlData.AppendLine(
@"<root>"); 

foreach (DataRow dr in dt.Rows) 

string id = dr["id"].ToString(); 
string name = dr["name"].ToString(); 

xmlData.AppendLine(
@"<person"); 
xmlData.AppendLine(
@" id='"+id+""); 
xmlData.AppendLine(
@">"); 

xmlData.AppendLine(
@"<id>" + id + "</id>"); 
xmlData.AppendLine(
@"<name>" + name + "</name>"); 

xmlData.AppendLine(
@"</person>"); 
}
 
xmlData.AppendLine(
@"</root>"); 
Response.Write(xmlData.ToString()); 

}
 
private void getXml() 

StringBuilder xmlData 
= new StringBuilder(); 
xmlData.AppendLine(
@"<?xml version='1.0' encoding='UTF-8' ?>"); 
xmlData.AppendLine(
@"<root>根节点</root>"); 
xmlData.AppendLine(
@"根节点" + DateTime.Now.ToString() + ""); 
xmlData.AppendLine(
@"</root>"); 

Response.Write(xmlData.ToString()); 
}
 
}
  

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

日历

最新评论及回复

最近发表

Powered By Sam

Copyright 2007-2008 小光. Some Rights Reserved.