(2)讀取XML文件,並顯示出來
讀取XML是通過創建“XmlNodeReader”對象來實現的,“XmlNodeReader”對象主要是用來可以讀取XML的節點數據。在本文的程序中使用到了一些“XmlNodeReader”的屬性,譬如“NodeType”屬性,用來判斷讀取得節點是什麼類型。“Value”是節點的值。下面是讀取XML文件並顯示在ListView中的實現代碼,其中listview1是已經創建ListVIEw組件:
while ( reader.Read ( ) )
{
//判斷當前讀取得節點類型
switch ( reader.NodeType )
{
case XMLNodeType.Element :
s = reader.Name ;
break ;
case XMLNodeType.Text :
if ( s.Equals ( "Name" ) )
myItem = ListvIEw1.Items.Add ( reader.Value ) ;
else
myItem.SubItems.Add ( reader.Value ) ;
break ;
}
}
四.C#讀取XML的文件的源程序代碼(read.cs)
在了解了上面的內容以後,
可以得到用C#讀取指定XML文件的完整代碼,具體如下:
using System ;
using System.Drawing ;
using System.Collections ;
using System.ComponentModel ;
using System.Windows.Forms ;
using System.Data ;
using System.XML ;
public class Form1 : Form
{
private Button button1 ;
private ListView ListvIEw1 ;
private System.ComponentModel.Container components = null ;
public Form1 ( )
{
//初始化窗體中的各個組件
InitializeComponent ( ) ;
}
//清除程序中使用過的資源
protected override void Dispose ( bool disposing )
{
if ( disposing )
{
if ( components != null )
{
components.Dispose ( ) ;
}
}
base.Dispose ( disposing ) ;
}
private void InitializeComponent ( )
{
button1 = new Button ( ) ;
Listview1 = new ListVIEw ( ) ;
SuspendLayout ( ) ;
button1.Anchor = ( ( AnchorStyles.Bottom | AnchorStyles.Left )
| AnchorStyles.Right ) ;
button1.Location = new Point ( 240 , 296 ) ;
button1.Name = "button1" ;
button1.Size = new Size ( 112 , 37 ) ;
button1.TabIndex = 0 ;
button1.Text = "讀取XML文檔" ;
button1.Click += new System.EventHandler ( button1_Click ) ;
ListvIEw1.Anchor = ( ( ( AnchorStyles.Top | AnchorStyles.Bottom )
| AnchorStyles.Left )
| AnchorStyles.Right ) ;
ListvIEw1.GridLines = true ;
ListvIEw1.Location = new Point ( 10 , 9 ) ;
Listview1.Name = "ListvIEw1" ;
ListvIEw1.Size = new Size ( 623 , 269 ) ;
ListvIEw1.TabIndex = 1 ;
Listview1.View = VIEw.Details ;
this.AutoScaleBaseSize = new Size ( 6 , 14 ) ;
this.ClIEntSize = new Size ( 608 , 348 ) ;
this.Controls.Add ( ListvIEw1 );
this.Controls.Add ( button1 );
this.Name = "Form1" ;
this.StartPosition = FormStartPosition.CenterScreen ;
this.Text = "用C#來讀取XML文檔" ;
this.ResumeLayout ( false ) ;
}
static void Main ( )
{
Application.Run ( new Form1 ( ) ) ;
}
private void button1_Click ( object sender , System.EventArgs e )
{
ListViewItem myItem = new ListVIEwItem ( ) ;
// 構建listvIEw組件
ListvIEw1.Columns.Clear ( ) ;
ListvIEw1.Items.Clear ( ) ;
ListvIEw1.Columns.Add ( "Name" , 80 , HorizontalAlignment.Left ) ;
ListvIEw1.Columns.Add ( "Zip" , 80 , HorizontalAlignment.Left ) ;
ListvIEw1.Columns.Add ( "Address" , 80 , HorizontalAlignment.Left ) ;
ListvIEw1.Columns.Add ( "City" , 80 , HorizontalAlignment.Left ) ;
ListvIEw1.Columns.Add ( "State" , 80 , HorizontalAlignment.Left ) ;
XMLNodeReader reader = null ;
try
{
string s = "" ;
XmlDocument doc = new XMLDocument ( ) ;
// 裝入指定的XML文檔
doc.Load ( "C:\\data.XML" ) ;
// 設定XmlNodeReader對象來打開XML文件
reader = new XMLNodeReader ( doc ) ;
// 讀取XML文件中的數據,並顯示出來
while ( reader.Read ( ) )
{
//判斷當前讀取得節點類型
switch ( reader.NodeType )
{
case XMLNodeType.Element :
s = reader.Name ;
break ;
case XMLNodeType.Text :
if ( s.Equals ( "Name" ) )
myItem = ListvIEw1.Items.Add ( reader.Value ) ;
else
myItem.SubItems.Add ( reader.Value ) ;
break ;
}
}
}
finally
{
//清除打開的數據流
if ( reader != null )
reader.Close ( ) ;
}
}
}
五.總結
C#和XML的淵源是很深的,本文只是從一個側面反映了二者關系的密切程度。在.Net FrameWork SDK中存在許多可以直接操作XML的類庫,掌握這些類庫的使用方法,對用C#開發和XML相關程序是十分必要的。