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#region Windows Form Designer generated code
54 /**//// <summary>
55 /// 设计器支持所需的方法 - 不要使用代码编辑器修改
56 /// 此方法的内容。
57 /// </summary>
58 private void InitializeComponent()
59 {
60 this.label1 = new System.Windows.Forms.Label();
61 this.dataGrid1 = new System.Windows.Forms.DataGrid();
62 this.panel1 = new System.Windows.Forms.Panel();
63 this.textBox1 = new System.Windows.Forms.TextBox();
64 this.btnShowSchema = new System.Windows.Forms.Button();
65 this.btnReadXML = new System.Windows.Forms.Button();
66 ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
67 this.panel1.SuspendLayout();
68 this.SuspendLayout();
69 //
70 // label1
71 //
72 this.label1.Dock = System.Windows.Forms.DockStyle.Top;
73 this.label1.Name = "label1";
74 this.label1.Size = new System.Drawing.Size(570, 23);
75 this.label1.TabIndex = 0;
76 this.label1.Text = "读取XML文件中的数据";
77 this.label1.TextAlign = System.Drawing.ContentAlignment.BottomCenter;
78 //
79 // dataGrid1
80 //
81 this.dataGrid1.DataMember = "";
82 this.dataGrid1.Dock = System.Windows.Forms.DockStyle.Fill;
83 this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
84 this.dataGrid1.Location = new System.Drawing.Point(0, 23);
85 this.dataGrid1.Name = "dataGrid1";
86 this.dataGrid1.Size = new System.Drawing.Size(570, 403);
87 this.dataGrid1.TabIndex = 1;
88 //
89 // panel1
90 //
91 this.panel1.Controls.AddRange(new System.Windows.Forms.Control[] {
92 this.textBox1,
93 this.btnShowSchema,
94 this.btnReadXML});
95 this.panel1.Dock = System.Windows.Forms.DockStyle.Bottom;
96 this.panel1.Location = new System.Drawing.Point(0, 258);
97 this.panel1.Name = "panel1";
98 this.panel1.Size = new System.Drawing.Size(570, 168);
99 this.panel1.TabIndex = 5;
100 //
101 // textBox1
102 //
103 this.textBox1.Dock = System.Windows.Forms.DockStyle.Top;
104 this.textBox1.Multiline = true;
105 this.textBox1.Name = "textBox1";
106 this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
107 this.textBox1.Size = new System.Drawing.Size(570, 120);
108 this.textBox1.TabIndex = 6;
109 this.textBox1.Text = "textBox1";
110 //
111 // btnShowSchema
112 //
113 this.btnShowSchema.Location = new System.Drawing.Point(160, 128);
114 this.btnShowSchema.Name = "btnShowSchema";
115 this.btnShowSchema.Size = new System.Drawing.Size(75, 32);
116 this.btnShowSchema.TabIndex = 5;
117 this.btnShowSchema.Text = "显示架构";
118 this.btnShowSchema.Click += new System.EventHandler(this.btnShowSchema_Click);
119 //
120 // btnReadXML
121 //
122 this.btnReadXML.Location = new System.Drawing.Point(24, 128);
123 this.btnReadXML.Name = "btnReadXML";
124 this.btnReadXML.Size = new System.Drawing.Size(88, 32);
125 this.btnReadXML.TabIndex = 4;
126 this.btnReadXML.Text = "读取XML";
127 this.btnReadXML.Click += new System.EventHandler(this.btnReadXML_Click);
128 //
129 // Form1
130 //
131 this.AutoScaleBaseSize = new System.Drawing.Size(8, 18);
132 this.ClientSize = new System.Drawing.Size(570, 426);
133 this.Controls.AddRange(new System.Windows.Forms.Control[] {
134 this.panel1,
135 this.dataGrid1,
136 this.label1});
137 this.Name = "Form1";
138 this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
139 this.Text = "读取XML数据";
140 ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
141 this.panel1.ResumeLayout(false);
142 this.ResumeLayout(false);
143
144 }
145 #endregion
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
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#region Windows Form Designer generated code
54 /**//// <summary>
55 /// 设计器支持所需的方法 - 不要使用代码编辑器修改
56 /// 此方法的内容。
57 /// </summary>
58 private void InitializeComponent()
59 {
60 this.label1 = new System.Windows.Forms.Label();
61 this.dataGrid1 = new System.Windows.Forms.DataGrid();
62 this.panel1 = new System.Windows.Forms.Panel();
63 this.textBox1 = new System.Windows.Forms.TextBox();
64 this.btnShowSchema = new System.Windows.Forms.Button();
65 this.btnReadXML = new System.Windows.Forms.Button();
66 ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
67 this.panel1.SuspendLayout();
68 this.SuspendLayout();
69 //
70 // label1
71 //
72 this.label1.Dock = System.Windows.Forms.DockStyle.Top;
73 this.label1.Name = "label1";
74 this.label1.Size = new System.Drawing.Size(570, 23);
75 this.label1.TabIndex = 0;
76 this.label1.Text = "读取XML文件中的数据";
77 this.label1.TextAlign = System.Drawing.ContentAlignment.BottomCenter;
78 //
79 // dataGrid1
80 //
81 this.dataGrid1.DataMember = "";
82 this.dataGrid1.Dock = System.Windows.Forms.DockStyle.Fill;
83 this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
84 this.dataGrid1.Location = new System.Drawing.Point(0, 23);
85 this.dataGrid1.Name = "dataGrid1";
86 this.dataGrid1.Size = new System.Drawing.Size(570, 403);
87 this.dataGrid1.TabIndex = 1;
88 //
89 // panel1
90 //
91 this.panel1.Controls.AddRange(new System.Windows.Forms.Control[] {
92 this.textBox1,
93 this.btnShowSchema,
94 this.btnReadXML});
95 this.panel1.Dock = System.Windows.Forms.DockStyle.Bottom;
96 this.panel1.Location = new System.Drawing.Point(0, 258);
97 this.panel1.Name = "panel1";
98 this.panel1.Size = new System.Drawing.Size(570, 168);
99 this.panel1.TabIndex = 5;
100 //
101 // textBox1
102 //
103 this.textBox1.Dock = System.Windows.Forms.DockStyle.Top;
104 this.textBox1.Multiline = true;
105 this.textBox1.Name = "textBox1";
106 this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
107 this.textBox1.Size = new System.Drawing.Size(570, 120);
108 this.textBox1.TabIndex = 6;
109 this.textBox1.Text = "textBox1";
110 //
111 // btnShowSchema
112 //
113 this.btnShowSchema.Location = new System.Drawing.Point(160, 128);
114 this.btnShowSchema.Name = "btnShowSchema";
115 this.btnShowSchema.Size = new System.Drawing.Size(75, 32);
116 this.btnShowSchema.TabIndex = 5;
117 this.btnShowSchema.Text = "显示架构";
118 this.btnShowSchema.Click += new System.EventHandler(this.btnShowSchema_Click);
119 //
120 // btnReadXML
121 //
122 this.btnReadXML.Location = new System.Drawing.Point(24, 128);
123 this.btnReadXML.Name = "btnReadXML";
124 this.btnReadXML.Size = new System.Drawing.Size(88, 32);
125 this.btnReadXML.TabIndex = 4;
126 this.btnReadXML.Text = "读取XML";
127 this.btnReadXML.Click += new System.EventHandler(this.btnReadXML_Click);
128 //
129 // Form1
130 //
131 this.AutoScaleBaseSize = new System.Drawing.Size(8, 18);
132 this.ClientSize = new System.Drawing.Size(570, 426);
133 this.Controls.AddRange(new System.Windows.Forms.Control[] {
134 this.panel1,
135 this.dataGrid1,
136 this.label1});
137 this.Name = "Form1";
138 this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
139 this.Text = "读取XML数据";
140 ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
141 this.panel1.ResumeLayout(false);
142 this.ResumeLayout(false);
143
144 }
145 #endregion
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