XML 学习 1 读写XML 2 使用文档对象建模(DOM) 3 对数据集使用XML 4 使用C# 编辑XML文件 可扩展标记语言已经(XML)已经成为标记共享数据的标准。 XML文件可以用于很多方面,包括标记标记的简单数据文件和存储临时数据的简单任务, 以及将信息从一个程序和进程传递到另一个数据和进程的复杂任务。 System.Xml 包括:System.Xml.Xpath System.Xml.Schema System.Xml.Xsl 程序中使用那个命名空间将取决于要实现的功能。 例如: 想使用部分XML文档,并操作字符串和数字。 包含System.Xml.XPath 执行构架映射和验证,System,,Xml.Schema 能将 XML数据转换成Html或者其他格式 System.Xml.Xsl 这些命名空间包括4类: (1) 执行基本的XML分析和编写操作的类 (2) 在文档,结点或者结构中编辑XML的类。 (3) 验证XML的类 (4) 转换XML的类 1 读写XML (1) 使用XMLTextWrite创建XML XMLTextWrite 创建XMLTextWrite 实例 重载列表 [C#] public XmlTextWriter(TextWriter); public XmlTextWriter(Stream, Encoding); public XmlTextWriter(string, Encoding); Encoding属性包括:System.Text..ASCIIEncoding, System.Text.UnicodeEncoding, System.Text.UTF7Encoding, System.Text.UTF8Encoding(包含在System.Text里面) 例如需要 在 C:/XmlData 目录下面创建一个myTextWrite.xml文件,可以使用下面的构造函数 XmlTextWriter myWriter=new XmlTextWriter("C:/XmlData/myTextWrite.xml",null) Xml文件包括:Xml文件声明,注释,元素,子元素,属性 1 创建XML声明: myWriter.WriteStartDocument(); //对这个方法调用打开一个新的XML文档,并在XML文件和流中 写入一下文本:<?xml version="1.0" encoding="utf--8"?> 2 添加注释。尽管不是必要在Xml中添加注释,但是这样做最好.注释:<!--...--> [C#] public override void WriteComment(string text); 例如:myWriter.WriterComment("comment text");//调用这个方法产生<!--comment text--> WriteComment方法使用合适的XML标记编排注释格式 3 编写元素和子元素 XML文件最重要的部分是包含元素的区域,主要数据都存储在元素中. 有2种技术用于创建元素,可以同时编写元素和他的值,或者打开元素,写入元素的内容,然后关闭元素. (1)利用WriteElementString方法我们能同时编写元素和他的值. XmlWriter.WriteElementString 方法 当在派生类中被重写时,写出包含字符串值的元素。 格式: public void WriteElementString(string, string); 这里第一个参数是要创建元素的元素名称, 第二个参数是元素的值 例如: myWriter.WriteElementString("Video","Gone with the wind");//产生的XML是<Video>Gone With the wind</Video> 这个方法编写了Video元素的开始标记符,内容和结束标记符。 如果想创建包含子元素的元素,或者想在一个元素里面添加属性,那么必须打开元素,编写标识符和其他一切 数据.调用不带参数的WriteEndElement方法,创建结束标识符,关闭这个元素. 例如: myWriter.WriteStartElement("Video"); myWriter.WriteString("Gone with thw wind"); myWriter.WriteEndElement();
public void WriteElementString(string, string, string); (2)添加属性 XmlTextWriter类还可以通过WriteAttributeString方法创建属性. XmlTextWriter 中的属性命名空间前缀(来自msdn) 有多种方法可以处理 XmlTextWriter 的属性 WriteAttributes、WriteAttributeString 和 WriteStartAttribute 的命名空间前缀。 WriteAttributes 如果当前位置是元素节点,则 WriteAttributes 方法写出在 XmlReader 中的当前位置找到的所有属性。如果 XmlReader 声明了命名空间前缀,则除属性之外 WriteAttributes 方法还写出该命名空间前缀。下面的代码示例显示 WriteAttributes 方法如何写出属性的命名空间前缀。 例如:myWriter.WriteStartElement("Length"); myWriter.WriteAttributeString("Measurement","Minutes"); myWriter.WriteString("120"); myWriter.WriteEndElement(); //这段代码产生<Length Measurement="Minutes">120</Length> 更高级的办法是WriteStartAttribute和WriteEndAttribute方法进行更多的属性定义。 (3)XML文件中添加格式和空格 XML不需要任何特别的空格或者缩进,但是添加格式和空格可以使XML文件查看起来更容易。 创建XML文件的时候,可能需要 设置XMLTextWriter对象的以下4个属性 Formatting Indentation IndentChar QuoteChar XmlTextWriter.Formatting 属性 指示如何对输出进行格式设置。public Formatting Formatting {get; set;} 属性值 Formatting 值之一。默认值为 Formatting.None(不进行特殊的格式设置)。 备注 如果设置了“缩进”选项,则使用 Indentation 和 IndentChar 属性对子元素进行缩进。只缩进元素内容。下面的 C# 代码写出包括混合内容的 HTML 元素: 例如: XmlTextWriter w = new XmlTextWriter(Console.Out); w.Formatting = Formatting.Indented; w.WriteStartElement("ol"); w.WriteStartElement("li"); w.WriteString("The big "); // This means "li" now has a mixed content model. w.WriteElementString("b", "E"); w.WriteElementString("i", "lephant"); w.WriteString(" walks slowly."); w.WriteEndElement(); w.WriteEndElement(); 上面的代码产生下面的输出: <ol> <li>The big <b>E</b><i>lephant</i> walks slowly.</li> </ol> (4) 关闭XML文件或者流 myWriter.WriteEndDocument(); myWriter.Close();
使用XmlTextWriter的应用程序示例()
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Xml; using System.Text; namespace XMLApp { /**//// <summary> /// Form1 的摘要说明。 /// </summary> public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.Label label3; private System.Windows.Forms.Label label4; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.TextBox textBox2; private System.Windows.Forms.TextBox textBox3; private System.Windows.Forms.ComboBox comboBox1; private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; /**//// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null; private System.Xml.XmlTextWriter xtw; public Form1() { // // Windows 窗体设计器支持所必需的 // InitializeComponent(); xtw=new XmlTextWriter("temp.xml",null); xtw.Formatting=Formatting.Indented; xtw.Indentation=3; xtw.WriteStartDocument(); xtw.WriteComment("Video Library"); xtw.WriteComment("This is a file containing a number of videos"); xtw.WriteStartElement("VideoLibrary"); // // TODO: 在 InitializeComponent 调用后添加任何构造函数代码 // } /**//// <summary> /// 清理所有正在使用的资源。 /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码 /**//// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.label3 = new System.Windows.Forms.Label(); this.label4 = new System.Windows.Forms.Label(); this.textBox1 = new System.Windows.Forms.TextBox(); this.textBox2 = new System.Windows.Forms.TextBox(); this.textBox3 = new System.Windows.Forms.TextBox(); this.comboBox1 = new System.Windows.Forms.ComboBox(); this.button1 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // label1 // this.label1.Location = new System.Drawing.Point(16, 16); this.label1.Name = "label1"; this.label1.TabIndex = 0; this.label1.Text = "Title"; // // label2 // this.label2.Location = new System.Drawing.Point(16, 56); this.label2.Name = "label2"; this.label2.TabIndex = 1; this.label2.Text = "Length"; // // label3 // this.label3.Location = new System.Drawing.Point(16, 104); this.label3.Name = "label3"; this.label3.TabIndex = 2; this.label3.Text = "Star"; // // label4 // this.label4.Location = new System.Drawing.Point(8, 152); this.label4.Name = "label4"; this.label4.TabIndex = 3; this.label4.Text = "Rating"; // // textBox1 // this.textBox1.Location = new System.Drawing.Point(152, 16); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(176, 20); this.textBox1.TabIndex = 4; this.textBox1.Text = ""; // // textBox2 // this.textBox2.Location = new System.Drawing.Point(152, 56); this.textBox2.Name = "textBox2"; this.textBox2.Size = new System.Drawing.Size(176, 20); this.textBox2.TabIndex = 5; this.textBox2.Text = ""; // // textBox3 // this.textBox3.Location = new System.Drawing.Point(152, 96); this.textBox3.Name = "textBox3"; this.textBox3.Size = new System.Drawing.Size(176, 20); this.textBox3.TabIndex = 6; this.textBox3.Text = ""; // // comboBox1 // this.comboBox1.Location = new System.Drawing.Point(152, 152); this.comboBox1.Name = "comboBox1"; this.comboBox1.Size = new System.Drawing.Size(128, 21); this.comboBox1.TabIndex = 7; this.comboBox1.Text = "comboBox1"; // // button1 // this.button1.Location = new System.Drawing.Point(56, 192); this.button1.Name = "button1"; this.button1.TabIndex = 8; this.button1.Text = "Add"; this.button1.Click += new System.EventHandler(this.button1_Click); // // button2 // this.button2.Location = new System.Drawing.Point(200, 192); this.button2.Name = "button2"; this.button2.TabIndex = 9; this.button2.Text = "Exit"; this.button2.Click += new System.EventHandler(this.button2_Click); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(344, 230); this.Controls.Add(this.button2); this.Controls.Add(this.button1); this.Controls.Add(this.comboBox1); this.Controls.Add(this.textBox3); this.Controls.Add(this.textBox2); this.Controls.Add(this.textBox1); this.Controls.Add(this.label4); this.Controls.Add(this.label3); this.Controls.Add(this.label2); this.Controls.Add(this.label1); this.Name = "Form1"; this.Text = "Form1"; this.Load += new System.EventHandler(this.Form1_Load); this.Closed += new System.EventHandler(this.Quit); this.ResumeLayout(false); } #endregion /**//// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } private void Form1_Load(object sender, System.EventArgs e) { comboBox1.Items.Clear(); comboBox1.Items.Add("A1"); comboBox1.Items.Add("A2"); comboBox1.Items.Add("A3"); comboBox1.Items.Add("A4"); comboBox1.SelectedIndex=0; } private void Quit(object sender,System.EventArgs e) { xtw.WriteEndElement(); xtw.WriteEndDocument(); xtw.Flush(); xtw.Close(); Application.Exit(); } private void button2_Click(object sender, System.EventArgs e) { Quit(null,EventArgs.Empty); } private void button1_Click(object sender, System.EventArgs e) { xtw.WriteStartElement("Video"); xtw.WriteElementString("TiTle",textBox1.Text); #region xtw.WriteStartElement("Length"); xtw.WriteAttributeString("Measurement","minutes"); xtw.WriteString(textBox2.Text); xtw.WriteEndElement(); #endregion xtw.WriteElementString("Star",textBox3.Text); xtw.WriteElementString("rating",comboBox1.SelectedText); xtw.WriteEndElement(); textBox1.Focus(); textBox1.Text=""; textBox2.Text=""; textBox3.Text=""; } } }