這篇文章主要講解了“C#怎么讀取XML文件”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“C#怎么讀取XML文件”吧!
成都創(chuàng)新互聯(lián)公司堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿(mǎn)足客戶(hù)于互聯(lián)網(wǎng)時(shí)代的沁陽(yáng)網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
用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 ( ) { //初始化窗體中的各個(gè)組件 InitializeComponent ( ) ; } //清除程序中使用過(guò)的資源 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#來(lái)讀取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" ) ; // 設(shè)定XmlNodeReader對(duì)象來(lái)打開(kāi)XML文件 reader = new XmlNodeReader ( doc ) ; // 讀取XML文件中的數(shù)據(jù),并顯示出來(lái) while ( reader.Read ( ) ) { //判斷當(dāng)前讀取得節(jié)點(diǎn)類(lèi)型 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 { //清除打開(kāi)的數(shù)據(jù)流 if ( reader != null ) reader.Close ( ) ; }}}
感謝各位的閱讀,以上就是“C#怎么讀取XML文件”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)C#怎么讀取XML文件這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!