C#读取XML文件源程序代码

C#读取XML文件源程序代码(read.cs)

目前累计服务客户千余家,积累了丰富的产品开发及服务经验。以网站设计水平和技术实力,树立企业形象,为客户提供网站制作、网站建设、网站策划、网页设计、网络营销、VI设计、网站改版、漏洞修补等服务。创新互联建站始终以务实、诚信为根本,不断创新和提高建站品质,通过对领先技术的掌握、对创意设计的研究、对客户形象的视觉传递、对应用系统的结合,为客户提供更好的一站式互联网解决方案,携手广大客户,共同发展进步。

在了解了上面的内容以后,可以得到用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. //初始化窗体中的各个组件  
  17. InitializeComponent ( ) ;  
  18. }  
  19. //清除程序中使用过的资源  
  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#来读取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. // 构建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. // 设定XmlNodeReader对象来打开XML文件  
  91. reader = new XmlNodeReader ( doc ) ;  
  92. // 读取XML文件中的数据,并显示出来  
  93. while ( reader.Read ( ) )   
  94. {  
  95. //判断当前读取得节点类型  
  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. //清除打开的数据流  
  112. if ( reader != null )  
  113. reader.Close ( ) ;  
  114. }
  115. }
  116. }

以上介绍C#读取XML文件源程序代码

C#和XML的渊源是很深的,本文只是从一个侧面反映了二者关系的密切程度。在.Net FrameWork SDK中存在许多可以直接操作XML的类库,掌握这些类库的使用方法,对用C#开发和XML相关程序是十分必要的。

名称栏目:C#读取XML文件源程序代码
当前地址:http://www.csdahua.cn/qtweb/news45/500495.html

网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网