?? recordxml.aspx.cs
字號:
/*
C#發(fā)現(xiàn)之旅系列教程配套演示代碼
本代碼僅供學習和參考使用
編制 袁永福 2008-5-15
MSN yyf9989@hotmail.com
QQ 28348092
作者博客 http://xdesigner.cnblogs.com/
使用者請作者的尊重知識產(chǎn)權(quán)。
*/
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace cs_discovery
{
/// <summary>
/// 使用XmlTextWriter 輸出XML文檔內(nèi)容,并允許在瀏覽器客戶端執(zhí)行XSLT轉(zhuǎn)換的ASPX頁面
/// </summary>
public class recordxml : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
// 此處使用 XmlTextWriter 來快速輸出XML文檔內(nèi)容.不構(gòu)造XML文檔對象結(jié)構(gòu)
this.Response.ContentEncoding = System.Text.Encoding.GetEncoding( 936 );
this.Response.ContentType = "text/xml";
// 連接數(shù)據(jù)庫
using( System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection())
{
conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
+ this.Server.MapPath("demomdb.mdb");
conn.Open();
// 查詢數(shù)據(jù)庫
using( System.Data.OleDb.OleDbCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "Select * From Customers";
System.Data.OleDb.OleDbDataReader reader = cmd.ExecuteReader();
// 獲得所有字段名
int FieldCount = reader.FieldCount ;
string[] FieldNames = new string[ FieldCount ] ;
for( int iCount = 0 ; iCount < FieldCount ; iCount ++ )
{
FieldNames[ iCount ] = reader.GetName( iCount );
}
// 生成一個XML文檔書寫器
System.Xml.XmlTextWriter xmlwriter = new System.Xml.XmlTextWriter( this.Response.Output );
xmlwriter.Indentation = 3 ;
xmlwriter.IndentChar = ' ';
xmlwriter.Formatting = System.Xml.Formatting.Indented ;
// 開始輸出XML文檔
xmlwriter.WriteStartDocument();
// 輸出XSLT樣式表信息頭
string strXSLRef = this.Request.QueryString["xsl"] ;
if( strXSLRef != null && strXSLRef.Length > 0 )
{
xmlwriter.WriteProcessingInstruction(
"xml-stylesheet" ,
"type='text/xsl' href='" + strXSLRef + "'");
}
xmlwriter.WriteStartElement("Table");
while( reader.Read())
{
// 輸出一條記錄
xmlwriter.WriteStartElement("Record");
for( int iCount = 0 ; iCount < FieldCount ; iCount ++ )
{
// 輸出一個字段值
xmlwriter.WriteStartElement( FieldNames[ iCount ] );
object v = reader.GetValue( iCount );
if( v == null || DBNull.Value.Equals( v ))
{
xmlwriter.WriteAttributeString("Null" , "1");
}
else
{
xmlwriter.WriteString( Convert.ToString( v ));
}
xmlwriter.WriteEndElement();
}
xmlwriter.WriteEndElement();
}//while( reader.Read())
reader.Close();
xmlwriter.WriteEndElement();
xmlwriter.WriteEndDocument();
xmlwriter.Close();
}//using( System.Data.OleDb.OleDbCommand cmd = conn.CreateCommand())
}//using( System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection())
}
#region Web 窗體設(shè)計器生成的代碼
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 該調(diào)用是 ASP.NET Web 窗體設(shè)計器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 設(shè)計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內(nèi)容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -