读取XML文件并将其内容显示在DataGrid组件中
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![]()
8
namespace ReadingXML
9
{
10
/// <summary>
11
/// 读取XML文件并将其内容显示在DataGrid组件中。
12
/// </summary>
13
public class Form1 : System.Windows.Forms.Form
14
{
15
private System.Windows.Forms.Label label1;
16
private System.Windows.Forms.DataGrid dataGrid1;
17
private System.Windows.Forms.Panel panel1;
18
private System.Windows.Forms.Button btnReadXML;
19
private System.Windows.Forms.Button btnShowSchema;
20
private System.Windows.Forms.TextBox textBox1;
21
/// <summary>
22
/// 必需的设计器变量。
23
/// </summary>
24
private System.ComponentModel.Container components = null;
25![]()
26
public Form1()
27
{
28
//
29
// Windows 窗体设计器支持所必需的
30
//
31
InitializeComponent();
32![]()
33
//
34
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
35
//
36
}
37![]()
38
/// <summary>
39
/// 清理所有正在使用的资源。
40
/// </summary>
41
protected override void Dispose( bool disposing )
42
{
43
if( disposing )
44
{
45
if (components != null)
46
{
47
components.Dispose();
48
}
49
}
50
base.Dispose( disposing );
51
}
52![]()
53
Windows Form Designer generated code
146![]()
147![]()
148![]()
149
/// <summary>
150
/// 应用程序的主入口点。
151
/// </summary>
152
[STAThread]
153
static void Main()
154
{
155
Application.Run(new Form1());
156
}
157![]()
158
// 申明一个数据集
159
DataSet dsAddress = new DataSet("address");
160
// 读取XML文件,并将读到的数据内容显示在DataGrid组件中。
161
private void btnReadXML_Click(object sender, System.EventArgs e)
162
{
163
string filePath = "address.xml";
164
dsAddress.ReadXml(filePath);
165
dataGrid1.DataSource = dsAddress;
166
dataGrid1.DataMember = "address";
167
dataGrid1.CaptionText = dataGrid1.DataMember;
168
}
169
// 显示XML文件的框件结构
170
private void btnShowSchema_Click(object sender, System.EventArgs e)
171
{
172
System.IO.StringWriter swXML = new System.IO.StringWriter();
173
dsAddress.WriteXmlSchema(swXML);
174
textBox1.Text = swXML.ToString();
175
}
176
}
177
}
178![]()
using System;2
using System.Drawing;3
using System.Collections;4
using System.ComponentModel;5
using System.Windows.Forms;6
using System.Data;7

8
namespace ReadingXML9
{10
/// <summary>11
/// 读取XML文件并将其内容显示在DataGrid组件中。12
/// </summary>13
public class Form1 : System.Windows.Forms.Form14
{15
private System.Windows.Forms.Label label1;16
private System.Windows.Forms.DataGrid dataGrid1;17
private System.Windows.Forms.Panel panel1;18
private System.Windows.Forms.Button btnReadXML;19
private System.Windows.Forms.Button btnShowSchema;20
private System.Windows.Forms.TextBox textBox1;21
/// <summary>22
/// 必需的设计器变量。23
/// </summary>24
private System.ComponentModel.Container components = null;25

26
public Form1()27
{28
//29
// Windows 窗体设计器支持所必需的30
//31
InitializeComponent();32

33
//34
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码35
//36
}37

38
/// <summary>39
/// 清理所有正在使用的资源。40
/// </summary>41
protected override void Dispose( bool disposing )42
{43
if( disposing )44
{45
if (components != null) 46
{47
components.Dispose();48
}49
}50
base.Dispose( disposing );51
}52

53
Windows Form Designer generated code146

147

148

149
/// <summary>150
/// 应用程序的主入口点。151
/// </summary>152
[STAThread]153
static void Main() 154
{155
Application.Run(new Form1());156
}157

158
// 申明一个数据集159
DataSet dsAddress = new DataSet("address");160
// 读取XML文件,并将读到的数据内容显示在DataGrid组件中。161
private void btnReadXML_Click(object sender, System.EventArgs e)162
{163
string filePath = "address.xml";164
dsAddress.ReadXml(filePath);165
dataGrid1.DataSource = dsAddress;166
dataGrid1.DataMember = "address";167
dataGrid1.CaptionText = dataGrid1.DataMember;168
}169
// 显示XML文件的框件结构170
private void btnShowSchema_Click(object sender, System.EventArgs e)171
{172
System.IO.StringWriter swXML = new System.IO.StringWriter();173
dsAddress.WriteXmlSchema(swXML);174
textBox1.Text = swXML.ToString();175
}176
}177
}178



浙公网安备 33010602011771号