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 ( ) ;
}
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
- 115.
- 116.
- 117.
以上介绍C#读取XML文件源程序代码
C#和XML的渊源是很深的,本文只是从一个侧面反映了二者关系的密切程度。在.Net FrameWork SDK中存在许多可以直接操作XML的类库,掌握这些类库的使用方法,对用C#开发和XML相关程序是十分必要的。
【编辑推荐】