XmlWriterSample1.cs
上传用户:lxycoco
上传日期:2022-07-21
资源大小:38457k
文件大小:3k
源码类别:

C#编程

开发平台:

Others

  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 Wrox.ProCSharp.Xml.XmlWriterSample1
  9. {
  10. /// <summary>
  11. /// Summary description for Form1.
  12. /// </summary>
  13. public class Form1 : System.Windows.Forms.Form
  14. {
  15. private System.Windows.Forms.Button button1;
  16. private System.Windows.Forms.ListBox listBox1;
  17. /// <summary>
  18. /// Required designer variable.
  19. /// </summary>
  20. private System.ComponentModel.Container components = null;
  21. public Form1()
  22. {
  23. //
  24. // Required for Windows Form Designer support
  25. //
  26. InitializeComponent();
  27. //
  28. // TODO: Add any constructor code after InitializeComponent call
  29. //
  30. }
  31. /// <summary>
  32. /// Clean up any resources being used.
  33. /// </summary>
  34. protected override void Dispose( bool disposing )
  35. {
  36. if( disposing )
  37. {
  38. if (components != null) 
  39. {
  40. components.Dispose();
  41. }
  42. }
  43. base.Dispose( disposing );
  44. }
  45. #region Windows Form Designer generated code
  46. /// <summary>
  47. /// Required method for Designer support - do not modify
  48. /// the contents of this method with the code editor.
  49. /// </summary>
  50. private void InitializeComponent()
  51. {
  52. this.button1 = new System.Windows.Forms.Button();
  53. this.listBox1 = new System.Windows.Forms.ListBox();
  54. this.SuspendLayout();
  55. // 
  56. // button1
  57. // 
  58. this.button1.Location = new System.Drawing.Point(40, 256);
  59. this.button1.Name = "button1";
  60. this.button1.Size = new System.Drawing.Size(200, 24);
  61. this.button1.TabIndex = 1;
  62. this.button1.Text = "Write XML";
  63. this.button1.Click += new System.EventHandler(this.button1_Click);
  64. // 
  65. // listBox1
  66. // 
  67. this.listBox1.Location = new System.Drawing.Point(24, 16);
  68. this.listBox1.Name = "listBox1";
  69. this.listBox1.Size = new System.Drawing.Size(232, 225);
  70. this.listBox1.TabIndex = 0;
  71. // 
  72. // Form1
  73. // 
  74. this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  75. this.ClientSize = new System.Drawing.Size(288, 293);
  76. this.Controls.AddRange(new System.Windows.Forms.Control[] {
  77.   this.listBox1,
  78.   this.button1});
  79. this.Name = "Form1";
  80. this.Text = "Form1";
  81. this.ResumeLayout(false);
  82. }
  83. #endregion
  84. /// <summary>
  85. /// The main entry point for the application.
  86. /// </summary>
  87. [STAThread]
  88. static void Main() 
  89. {
  90. Application.Run(new Form1());
  91. }
  92.   
  93. // XmlWriterSample1/form.cs
  94. private void button1_Click(object sender, System.EventArgs e)
  95. {
  96. // change to match you path structure
  97. string fileName="booknew.xml";
  98. //create the XmlTextWriter
  99. XmlTextWriter tw=new XmlTextWriter(fileName,null);
  100. //set the formatting to indented
  101. tw.Formatting=Formatting.Indented;
  102. tw.WriteStartDocument();
  103. //Start creating elements and attributes
  104. tw.WriteStartElement("book");
  105. tw.WriteAttributeString("genre","Mystery");
  106. tw.WriteAttributeString("publicationdate","2001");
  107. tw.WriteAttributeString("ISBN","123456789");
  108. tw.WriteElementString("title","Case of the Missing Cookie");
  109. tw.WriteStartElement("author");
  110. tw.WriteElementString("name","Cookie Monster");
  111. tw.WriteEndElement();
  112. tw.WriteElementString("price","9.99");
  113. tw.WriteEndElement();
  114. tw.WriteEndDocument();
  115. //clean up
  116. tw.Flush();
  117. tw.Close();
  118.     
  119. }
  120. }
  121. }