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

📄 readxml.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 ReadXML
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	public class ReadXML : 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 buttonExit;
		#endregion

		private XmlTextReader xmlTextReader;
		private System.Windows.Forms.Button buttonNext;

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

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

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

			this.xmlTextReader = new XmlTextReader(@"E:\WriteXML\VideoLibrary.xml"); //创建XmlTextReader对象


			if ( !this.DisplayData() )
			{
				MessageBox.Show("XML文件没有元素!");
			}

			
		}

		/// <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.buttonNext = 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";
			// 
			// buttonNext
			// 
			this.buttonNext.Location = new System.Drawing.Point(33, 193);
			this.buttonNext.Name = "buttonNext";
			this.buttonNext.Size = new System.Drawing.Size(160, 52);
			this.buttonNext.TabIndex = 8;
			this.buttonNext.Text = "Next";
			this.buttonNext.Click += new System.EventHandler(this.buttonNext_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);
			// 
			// ReadXML
			// 
			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.buttonNext);
			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 = "ReadXML";
			this.Text = "Form1";
			this.ResumeLayout(false);

		}
		#endregion

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

		private void buttonNext_Click(object sender, System.EventArgs e)
		{		

			if ( !this.DisplayData() )
			{
				MessageBox.Show("没有元素了!");
			}
				
		}

		private void buttonExit_Click(object sender, System.EventArgs e)
		{
			this.xmlTextReader.Close();
			Application.Exit();
		}

		protected bool DisplayData()
		{
			while ( this.xmlTextReader.Read())  //读取下一个节点
			{
				if ( this.xmlTextReader.MoveToContent() == XmlNodeType.Element && this.xmlTextReader.Name == "Title")
				{
					this.textBoxVideo.Text = this.xmlTextReader.ReadElementString();  //读出Title
				}

				if ( this.xmlTextReader.MoveToContent() == XmlNodeType.Element && this.xmlTextReader.Name == "Length")
				{
					this.textBoxLength.Text = this.xmlTextReader.ReadElementString(); //读出Length
				}

				if ( this.xmlTextReader.MoveToContent() == XmlNodeType.Element && this.xmlTextReader.Name == "Star")
				{
					this.textBoxStar.Text = this.xmlTextReader.ReadElementString(); //读出Star
					return true;
				}

			}

			return false; //XML中的读操作结束
			
		}

	
	}
}

⌨️ 快捷键说明

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