Form1.cs
Upload User: chinafred
Upload Date: 2007-08-14
Package Size: 10127k
Code Size: 7k
Category:

ADO-ODBC

Development Platform:

C#

  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. using System.Xml;
  8. namespace ch9_4
  9. {
  10. /// <summary>
  11. /// Form1 的摘要说明。
  12. /// </summary>
  13. public class Form1 : System.Windows.Forms.Form
  14. {
  15. private System.Windows.Forms.Label label1;
  16. private System.Windows.Forms.TextBox textBox1;
  17. private System.Windows.Forms.Button button1;
  18. private System.Windows.Forms.RichTextBox richTextBox1;
  19. private System.Windows.Forms.Button button2;
  20. private System.Windows.Forms.GroupBox groupBox1;
  21. /// <summary>
  22. /// 必需的设计器变量。
  23. /// </summary>
  24. private System.ComponentModel.Container components = null;
  25. public Form1()
  26. {
  27. //
  28. // Windows 窗体设计器支持所必需的
  29. //
  30. InitializeComponent();
  31. //
  32. // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  33. //
  34. }
  35. /// <summary>
  36. /// 清理所有正在使用的资源。
  37. /// </summary>
  38. protected override void Dispose( bool disposing )
  39. {
  40. if( disposing )
  41. {
  42. if (components != null) 
  43. {
  44. components.Dispose();
  45. }
  46. }
  47. base.Dispose( disposing );
  48. }
  49. #region Windows Form Designer generated code
  50. /// <summary>
  51. /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  52. /// 此方法的内容。
  53. /// </summary>
  54. private void InitializeComponent()
  55. {
  56. this.label1 = new System.Windows.Forms.Label();
  57. this.textBox1 = new System.Windows.Forms.TextBox();
  58. this.button1 = new System.Windows.Forms.Button();
  59. this.richTextBox1 = new System.Windows.Forms.RichTextBox();
  60. this.button2 = new System.Windows.Forms.Button();
  61. this.groupBox1 = new System.Windows.Forms.GroupBox();
  62. this.groupBox1.SuspendLayout();
  63. this.SuspendLayout();
  64. // 
  65. // label1
  66. // 
  67. this.label1.Location = new System.Drawing.Point(8, 16);
  68. this.label1.Name = "label1";
  69. this.label1.TabIndex = 0;
  70. this.label1.Text = "请选定XML文件";
  71. // 
  72. // textBox1
  73. // 
  74. this.textBox1.Location = new System.Drawing.Point(16, 40);
  75. this.textBox1.Name = "textBox1";
  76. this.textBox1.Size = new System.Drawing.Size(192, 21);
  77. this.textBox1.TabIndex = 1;
  78. this.textBox1.Text = "";
  79. // 
  80. // button1
  81. // 
  82. this.button1.Location = new System.Drawing.Point(232, 40);
  83. this.button1.Name = "button1";
  84. this.button1.Size = new System.Drawing.Size(64, 24);
  85. this.button1.TabIndex = 2;
  86. this.button1.Text = "浏览 ...";
  87. this.button1.Click += new System.EventHandler(this.button1_Click);
  88. // 
  89. // richTextBox1
  90. // 
  91. this.richTextBox1.Dock = System.Windows.Forms.DockStyle.Bottom;
  92. this.richTextBox1.Location = new System.Drawing.Point(0, 85);
  93. this.richTextBox1.Name = "richTextBox1";
  94. this.richTextBox1.Size = new System.Drawing.Size(408, 312);
  95. this.richTextBox1.TabIndex = 3;
  96. this.richTextBox1.Text = "";
  97. // 
  98. // button2
  99. // 
  100. this.button2.Location = new System.Drawing.Point(328, 40);
  101. this.button2.Name = "button2";
  102. this.button2.Size = new System.Drawing.Size(64, 23);
  103. this.button2.TabIndex = 4;
  104. this.button2.Text = "读取";
  105. this.button2.Click += new System.EventHandler(this.button2_Click);
  106. // 
  107. // groupBox1
  108. // 
  109. this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
  110. this.textBox1,
  111. this.button1,
  112. this.label1,
  113. this.button2});
  114. this.groupBox1.Dock = System.Windows.Forms.DockStyle.Top;
  115. this.groupBox1.Name = "groupBox1";
  116. this.groupBox1.Size = new System.Drawing.Size(408, 80);
  117. this.groupBox1.TabIndex = 5;
  118. this.groupBox1.TabStop = false;
  119. // 
  120. // Form1
  121. // 
  122. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  123. this.ClientSize = new System.Drawing.Size(408, 397);
  124. this.Controls.AddRange(new System.Windows.Forms.Control[] {
  125.   this.groupBox1,
  126.   this.richTextBox1});
  127. this.Name = "Form1";
  128. this.Text = "读取XML文档";
  129. this.groupBox1.ResumeLayout(false);
  130. this.ResumeLayout(false);
  131. }
  132. #endregion
  133. /// <summary>
  134. /// 应用程序的主入口点。
  135. /// </summary>
  136. [STAThread]
  137. static void Main() 
  138. {
  139. Application.Run(new Form1());
  140. }
  141. private void button1_Click(object sender, System.EventArgs e)
  142. {
  143. OpenFileDialog ofDialog=new OpenFileDialog();
  144. //设置过滤器
  145. ofDialog.Filter="XML文件(*.xml)|*.xml|所有文件(*.*)|*.*";
  146. if (ofDialog.ShowDialog()==DialogResult.OK)
  147. {
  148. //获取文件名
  149. this.textBox1.Text=ofDialog.FileName;
  150. }
  151. }
  152. private void button2_Click(object sender, System.EventArgs e)
  153. {
  154. //获取要读取的XML文件名
  155. string fileName=this.textBox1.Text.Trim();
  156. if (fileName!="")
  157. {
  158. //创建XmlTextReader读取器
  159. XmlTextReader reader=new XmlTextReader(fileName);
  160. this.richTextBox1.Text="";
  161. try
  162. {
  163. //读取文档中的所有节点
  164. while (reader.Read())
  165. {
  166. //根据节点的类型读取并输出相应的内容
  167. switch (reader.NodeType)
  168. {
  169. case XmlNodeType.XmlDeclaration:
  170. this.richTextBox1.Text+="<?" + reader.Name + " " + reader.Value + "?>";
  171. break;
  172. case XmlNodeType.Element:
  173. this.richTextBox1.Text+="<" + reader.Name;
  174. while (reader.MoveToNextAttribute())
  175. this.richTextBox1.Text+=" " + reader.Name + "="" + reader.Value + """;
  176. this.richTextBox1.Text+=">";
  177. break;
  178. case XmlNodeType.Text:
  179. this.richTextBox1.Text+=reader.Value;
  180. break;
  181. case XmlNodeType.CDATA:
  182. this.richTextBox1.Text+=reader.Value;
  183. break;
  184. case XmlNodeType.ProcessingInstruction:
  185. this.richTextBox1.Text+="<?" + reader.Name + " " + reader.Value + "?>";
  186. break;
  187. case XmlNodeType.Comment:
  188. this.richTextBox1.Text+="<!--" + reader.Value + "-->";
  189. break;
  190. case XmlNodeType.Document:
  191. this.richTextBox1.Text+="<?xml version='1.0'?>";
  192. break;
  193. case XmlNodeType.Whitespace:
  194. this.richTextBox1.Text+=reader.Value;
  195. break;
  196. case XmlNodeType.SignificantWhitespace:
  197. this.richTextBox1.Text+=reader.Value;
  198. break;
  199. case XmlNodeType.EndElement:
  200. this.richTextBox1.Text+="</" + reader.Name + ">";
  201. break;
  202. }
  203. }
  204. }
  205. catch(Exception excep)
  206. {
  207.     //显示错误
  208. MessageBox.Show("发生错误:"+excep.Message,"警告",MessageBoxButtons.OK,MessageBoxIcon.Warning);
  209. }
  210. }
  211. else
  212. {
  213. //没有选择文件
  214. MessageBox.Show("没有选择XML文件","警告",MessageBoxButtons.OK,MessageBoxIcon.Warning);
  215. }
  216. }
  217. }
  218. }