⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 writexml.cs

📁 设计一个XML文件 1. 利用XmlTextWriter对象完成文件写操作要求有界面
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Xml;

namespace WriteXML
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	public class WriteXML : System.Windows.Forms.Form
	{
		#region controls
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.TextBox textBoxVideo;
		private System.Windows.Forms.TextBox textBoxLength;
		private System.Windows.Forms.TextBox textBoxStar;
		private System.Windows.Forms.Button buttonAdd;
		private System.Windows.Forms.Button buttonExit;
		#endregion

		private XmlTextWriter xmlTextWriter;
		private System.Text.UTF8Encoding utf8 = new System.Text.UTF8Encoding();//编码方式

		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;

		public WriteXML()
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();

			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//

			this.xmlTextWriter = new XmlTextWriter(@"..\..\VideoLibrary.xml",utf8 ); //创建XmlTextWriter对象
			this.xmlTextWriter.WriteStartDocument(); //创建XML声明
			this.xmlTextWriter.WriteComment("This is a file containing a number of videos");//添加注释
			this.xmlTextWriter.WriteStartElement("VideoLiabrary");
		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows 窗体设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.textBoxVideo = new System.Windows.Forms.TextBox();
			this.textBoxLength = new System.Windows.Forms.TextBox();
			this.textBoxStar = new System.Windows.Forms.TextBox();
			this.label1 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.buttonAdd = new System.Windows.Forms.Button();
			this.buttonExit = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// textBoxVideo
			// 
			this.textBoxVideo.Location = new System.Drawing.Point(87, 30);
			this.textBoxVideo.Name = "textBoxVideo";
			this.textBoxVideo.Size = new System.Drawing.Size(166, 20);
			this.textBoxVideo.TabIndex = 0;
			this.textBoxVideo.Text = "";
			// 
			// textBoxLength
			// 
			this.textBoxLength.Location = new System.Drawing.Point(87, 67);
			this.textBoxLength.Name = "textBoxLength";
			this.textBoxLength.Size = new System.Drawing.Size(83, 20);
			this.textBoxLength.TabIndex = 1;
			this.textBoxLength.Text = "";
			// 
			// textBoxStar
			// 
			this.textBoxStar.Location = new System.Drawing.Point(87, 104);
			this.textBoxStar.Name = "textBoxStar";
			this.textBoxStar.Size = new System.Drawing.Size(166, 20);
			this.textBoxStar.TabIndex = 2;
			this.textBoxStar.Text = "";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(27, 30);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(41, 23);
			this.label1.TabIndex = 3;
			this.label1.Text = "Title";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(27, 67);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(41, 23);
			this.label2.TabIndex = 4;
			this.label2.Text = "Length";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(27, 104);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(41, 23);
			this.label3.TabIndex = 5;
			this.label3.Text = "Star";
			// 
			// buttonAdd
			// 
			this.buttonAdd.Location = new System.Drawing.Point(33, 193);
			this.buttonAdd.Name = "buttonAdd";
			this.buttonAdd.Size = new System.Drawing.Size(160, 52);
			this.buttonAdd.TabIndex = 8;
			this.buttonAdd.Text = "Add";
			this.buttonAdd.Click += new System.EventHandler(this.buttonAdd_Click);
			// 
			// buttonExit
			// 
			this.buttonExit.Location = new System.Drawing.Point(227, 193);
			this.buttonExit.Name = "buttonExit";
			this.buttonExit.Size = new System.Drawing.Size(46, 52);
			this.buttonExit.TabIndex = 9;
			this.buttonExit.Text = "Exit";
			this.buttonExit.Click += new System.EventHandler(this.buttonExit_Click);
			// 
			// WriteXML
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(313, 279);
			this.Controls.Add(this.buttonExit);
			this.Controls.Add(this.buttonAdd);
			this.Controls.Add(this.label3);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.textBoxStar);
			this.Controls.Add(this.textBoxLength);
			this.Controls.Add(this.textBoxVideo);
			this.Name = "WriteXML";
			this.Text = "Form1";
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new WriteXML());
		}

		private void buttonAdd_Click(object sender, System.EventArgs e)
		{
			string title = this.textBoxVideo.Text.Trim().ToString();
			string length = this.textBoxLength.Text.Trim().ToString();
			string star = this.textBoxStar.Text.Trim().ToString();

			//编写元素
			this.xmlTextWriter.WriteStartElement("Video");
			this.xmlTextWriter.WriteStartElement("Title");
            this.xmlTextWriter.WriteString(title);
            this.xmlTextWriter.WriteEndElement();

			this.xmlTextWriter.WriteStartElement("Length");
			this.xmlTextWriter.WriteAttributeString("Measurement", "minutes");
			this.xmlTextWriter.WriteString(length);
			this.xmlTextWriter.WriteEndElement();

			this.xmlTextWriter.WriteStartElement("Star");
			this.xmlTextWriter.WriteString(star);
			this.xmlTextWriter.WriteEndElement();
			this.xmlTextWriter.WriteEndElement();

			this.textBoxVideo.Text = "";
			this.textBoxLength.Text = "";
			this.textBoxStar.Text = "";		
		}

		private void buttonExit_Click(object sender, System.EventArgs e)
		{
			this.xmlTextWriter.WriteEndElement();
			this.xmlTextWriter.WriteEndDocument();
			this.xmlTextWriter.Flush(); //刷新流
			this.xmlTextWriter.Close(); //关闭xmlTextWriter对象
			Application.Exit();
		}

	
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -