在C#中如何读入一个xml文件,然后导入到treeview控件中中
3个回答
展开全部
<Employees>
<NO1>
<Name>Ma Jin Hu</Name>
<ZIP> 239000 </ZIP>
<Address>Feng Huang Street</Address>
<City>Chu Zhou City</City>
<State> ANHUI </State>
</NO1>
<NO2>
<Name>Wang Tian</Name>
<ZIP> 239000 </ZIP>
<Address>Lang Ya Street</Address>
<City>He Fei City</City>
<State> ANHUI </State>
</NO2>
<NO3>
<Name>Zou Wen Biao</Name>
<ZIP> 100000 </ZIP>
<Address>Sai Di Street</Address>
<City>Bei Jin City</City>
<State>Bei Jin</State>
</NO3>
</Employees>
1).装入XML文件,形成数据流:
通过创建一个"XmlDocument"对象,然后利用"load"方法,可以把XML文件装入,具体如下:
XmlDocument doc = new XmlDocument ( ) ;
// 装入指定的XML文档
doc.Load ( "C:\\data.xml" ) ;
(2).读取XML文件,并显示出来:
读取XML是通过创建"XmlNodeReader"对象来实现的,"XmlNodeReader"对象主要是用来可以读取XML的节点数据。在本文的程序中使用到了一些"XmlNodeReader"的属性,譬如"NodeType"属性,用来判断读取得节点是什么类型。"Value"是节点的值。下面是读取XML文件并显示在ListView中的实现代码,其中listview1是已经创建ListView组件:
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 ;
}
}
四.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 ( ) ;
}
}
}
自己 好好研究研究吧。
<NO1>
<Name>Ma Jin Hu</Name>
<ZIP> 239000 </ZIP>
<Address>Feng Huang Street</Address>
<City>Chu Zhou City</City>
<State> ANHUI </State>
</NO1>
<NO2>
<Name>Wang Tian</Name>
<ZIP> 239000 </ZIP>
<Address>Lang Ya Street</Address>
<City>He Fei City</City>
<State> ANHUI </State>
</NO2>
<NO3>
<Name>Zou Wen Biao</Name>
<ZIP> 100000 </ZIP>
<Address>Sai Di Street</Address>
<City>Bei Jin City</City>
<State>Bei Jin</State>
</NO3>
</Employees>
1).装入XML文件,形成数据流:
通过创建一个"XmlDocument"对象,然后利用"load"方法,可以把XML文件装入,具体如下:
XmlDocument doc = new XmlDocument ( ) ;
// 装入指定的XML文档
doc.Load ( "C:\\data.xml" ) ;
(2).读取XML文件,并显示出来:
读取XML是通过创建"XmlNodeReader"对象来实现的,"XmlNodeReader"对象主要是用来可以读取XML的节点数据。在本文的程序中使用到了一些"XmlNodeReader"的属性,譬如"NodeType"属性,用来判断读取得节点是什么类型。"Value"是节点的值。下面是读取XML文件并显示在ListView中的实现代码,其中listview1是已经创建ListView组件:
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 ;
}
}
四.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 ( ) ;
}
}
}
自己 好好研究研究吧。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询