C#讀取XML文件源程序代碼
作者:佚名 
  這里介紹C#讀取XML文件源程序代碼,在.Net FrameWork SDK中存在許多可以直接操作XML的類庫,掌握這些類庫的使用方法,對用C#開發(fā)和XML相關程序是十分必要的。
 C#讀取XML文件源程序代碼(read.cs)
在了解了上面的內(nèi)容以后,可以得到用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 ( ) ;
 - // 構(gòu)建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文件中的數(shù)據(jù),并顯示出來
 - while ( reader.Read ( ) )
 - {
 - //判斷當前讀取得節(jié)點類型
 - 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
 - {
 - //清除打開的數(shù)據(jù)流
 - if ( reader != null )
 - reader.Close ( ) ;
 - }
 - }
 - }
 
以上介紹C#讀取XML文件源程序代碼
C#和XML的淵源是很深的,本文只是從一個側(cè)面反映了二者關系的密切程度。在.Net FrameWork SDK中存在許多可以直接操作XML的類庫,掌握這些類庫的使用方法,對用C#開發(fā)和XML相關程序是十分必要的。
【編輯推薦】
責任編輯:佚名 
                    來源:
                    IT168
 














 
 
 
 
 
 
 