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 ( ) ;
}
}
}
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个命名空间和多个命名空间会有影响吗?
- C#跨线程调用COM对象问题
- 怎样不让窗体被无意关闭?我打算在窗体被点叉关闭的时候,要输入口令,正确才可以关闭,怎么做?
- DataGrid中如何直接在当前行进行编辑. 顺便散分
- 用webclient从FPT服务器下载文件问题
- 关于datagrid里面绑定dropdownlist。。急
- 有关于.NET类库详细介绍的书吗?
- 高手上吧,如何把一段C++代码和C#代码编译合并成一个DLL库文件 (见者有分哦)
- 问个小问题:DXDialog关闭时的判断问题
- MouseDown中用SendMessage改变控件大小
- 真是世态炎凉啊!..没分没人帮忙啊..
- WebRequest 读取本地iis中的网页有错
仔细研究一下System.Xml命名空间,里面有很多操作xml的类~