自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C#讀取XML文件源程序代碼

開(kāi)發(fā) 后端
這里介紹C#讀取XML文件源程序代碼,在.Net FrameWork SDK中存在許多可以直接操作XML的類(lèi)庫(kù),掌握這些類(lèi)庫(kù)的使用方法,對(duì)用C#開(kāi)發(fā)和XML相關(guān)程序是十分必要的。

C#讀取XML文件源程序代碼(read.cs)

在了解了上面的內(nèi)容以后,可以得到用C#讀取XML文件源程序代碼,具體如下:

  1. using System ;  
  2. using System.Drawing ;  
  3. using System.Collections ;  
  4. using System.ComponentModel ;  
  5. using System.Windows.Forms ;  
  6. using System.Data ;  
  7. using System.Xml ;  
  8. public class Form1 : Form  
  9. {  
  10. private Button button1 ;  
  11. private ListView Listview1 ;  
  12. private System.ComponentModel.Container components = null ;  
  13.    
  14. public Form1 ( )  
  15. {  
  16. //初始化窗體中的各個(gè)組件  
  17. InitializeComponent ( ) ;  
  18. }  
  19. //清除程序中使用過(guò)的資源  
  20. protected override void Dispose ( bool disposing )  
  21. {  
  22. if ( disposing )  
  23. {  
  24. if ( components != null )   
  25. {  
  26. components.Dispose ( ) ;  
  27. }  
  28. }  
  29. base.Dispose ( disposing ) ;  
  30. }  
  31. private void InitializeComponent ( )  
  32. {  
  33. button1 = new Button ( ) ;  
  34. Listview1 = new ListView ( ) ;  
  35. SuspendLayout ( ) ;  
  36.    
  37. button1.Anchor = ( ( AnchorStyles.Bottom | AnchorStyles.Left )   
  38. | AnchorStyles.Right ) ;  
  39. button1.Location = new Point ( 240 , 296 ) ;  
  40. button1.Name = "button1" ;  
  41. button1.Size = new Size ( 112 , 37 ) ;  
  42. button1.TabIndex = 0 ;  
  43. button1.Text = "讀取XML文檔" ;  
  44. button1.Click += new System.EventHandler ( button1_Click ) ;  
  45.    
  46. Listview1.Anchor = ( ( ( AnchorStyles.Top | AnchorStyles.Bottom )   
  47. | AnchorStyles.Left )   
  48. | AnchorStyles.Right ) ;  
  49. Listview1.GridLines = true ;  
  50. Listview1.Location = new Point ( 10 , 9 ) ;  
  51. Listview1.Name = "Listview1" ;  
  52. Listview1.Size = new Size ( 623 , 269 ) ;  
  53. Listview1.TabIndex = 1 ;  
  54. Listview1.View = View.Details ;  
  55.    
  56. this.AutoScaleBaseSize = new Size ( 6 , 14 ) ;  
  57. this.ClientSize = new Size ( 608 , 348 ) ;  
  58. this.Controls.Add ( Listview1 );  
  59. this.Controls.Add ( button1 );  
  60. this.Name = "Form1" ;  
  61. this.StartPosition = FormStartPosition.CenterScreen ;  
  62. this.Text = "用C#來(lái)讀取XML文檔" ;  
  63. this.ResumeLayout ( false ) ;  
  64.    
  65. }  
  66. static void Main ( )   
  67. {  
  68. Application.Run ( new Form1 ( ) ) ;  
  69. }  
  70.    
  71. private void button1_Click ( object sender , System.EventArgs e )  
  72. {  
  73. ListViewItem myItem = new ListViewItem ( ) ;  
  74. // 構(gòu)建listview組件  
  75. Listview1.Columns.Clear ( ) ;   
  76. Listview1.Items.Clear ( ) ;  
  77. Listview1.Columns.Add ( "Name" , 80 , HorizontalAlignment.Left ) ;   
  78. Listview1.Columns.Add ( "Zip" , 80 , HorizontalAlignment.Left ) ;   
  79. Listview1.Columns.Add ( "Address" , 80 , HorizontalAlignment.Left ) ;   
  80. Listview1.Columns.Add ( "City" , 80 , HorizontalAlignment.Left ) ;   
  81. Listview1.Columns.Add ( "State" , 80 , HorizontalAlignment.Left ) ;   
  82. XmlNodeReader reader = null ;  
  83.    
  84. try  
  85. {  
  86. string s = "" ;  
  87. XmlDocument doc = new XmlDocument ( ) ;  
  88. // 裝入指定的XML文檔  
  89. doc.Load ( "C:\\data.xml" ) ;  
  90. // 設(shè)定XmlNodeReader對(duì)象來(lái)打開(kāi)XML文件  
  91. reader = new XmlNodeReader ( doc ) ;  
  92. // 讀取XML文件中的數(shù)據(jù),并顯示出來(lái)  
  93. while ( reader.Read ( ) )   
  94. {  
  95. //判斷當(dāng)前讀取得節(jié)點(diǎn)類(lèi)型  
  96. switch ( reader.NodeType )  
  97. {  
  98. case XmlNodeType.Element :  
  99. s = reader.Name ;  
  100. break ;  
  101. case XmlNodeType.Text :  
  102. if ( s.Equals ( "Name" ) )  
  103. myItem = Listview1.Items.Add ( reader.Value ) ;  
  104. else  
  105. myItem.SubItems.Add ( reader.Value ) ;  
  106. break ;  
  107. }
  108. }
  109. finally  
  110. {  
  111. //清除打開(kāi)的數(shù)據(jù)流  
  112. if ( reader != null )  
  113. reader.Close ( ) ;  
  114. }
  115. }
  116. }

以上介紹C#讀取XML文件源程序代碼

C#和XML的淵源是很深的,本文只是從一個(gè)側(cè)面反映了二者關(guān)系的密切程度。在.Net FrameWork SDK中存在許多可以直接操作XML的類(lèi)庫(kù),掌握這些類(lèi)庫(kù)的使用方法,對(duì)用C#開(kāi)發(fā)和XML相關(guān)程序是十分必要的。

【編輯推薦】

  1. C#字符串操作步驟
  2. C#集成開(kāi)發(fā)環(huán)境淺析
  3. Visual C# .NET應(yīng)用程序
  4. C# TimeLabel控件詳解
  5. C#復(fù)合控件開(kāi)發(fā)技術(shù)
責(zé)任編輯:佚名 來(lái)源: IT168
相關(guān)推薦

2009-08-24 17:58:19

C#讀取XML文件

2009-08-11 13:48:11

C# ConfigDl

2009-09-02 18:28:00

C#鼠標(biāo)位置

2009-08-20 10:54:29

C#做瀏覽器源程序

2009-08-18 16:42:49

C# 操作XML

2009-08-12 18:29:06

C#讀取TXT文件

2009-08-12 15:26:38

C#讀取XML文檔

2009-08-12 16:26:30

C#讀取XML文檔

2010-01-15 10:48:29

C++程序代碼

2009-08-12 16:46:22

C#讀取XML文檔

2009-08-21 16:13:27

C#讀取資源文件

2009-08-13 09:32:00

C#讀取TXT文件

2009-08-25 11:10:20

C#編程實(shí)現(xiàn)顯示XML

2009-08-18 17:05:08

C#操作xml文件

2010-07-17 00:55:48

PHP Telnet

2009-09-09 18:00:55

C# XML編程

2009-09-09 18:20:29

C# XML編程

2010-01-15 18:46:08

C++程序代碼

2009-08-13 09:58:55

C#讀取配置文件

2009-08-13 09:16:57

C#讀取配置文件
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)