在.NET中编写XML(转) 一、利用.NET 类编写 XML 文档 XmlWriter 是个抽象类,所以不能直接使用该类中定义的成员,但是可以使用从XmlWrite类中派生的任何类中的成员。.NET Framework 提供的且从XmlWriter类中派生的唯一类是XmlTextWrite。 二、XmlWriter 类 XmlWriter 类是一个抽象类,它定义了编写XML时可用的基本方法和属性。 ● 确保XML 文档格式良好。 1、XmlWriter 方法 主要分析以下方法: ● WriteStartDocument()和WriteEndDocument()方法 下面分别研究: ①WriteStartDocument()和WriteEndDocument()方法 文档开头包括:版本号、文档是否独立、文档编码。 ②WriteStartElement()和WriteEndElement()方法 WriteStartElement()方法有3种重载: 一旦调用了WriteStartElement()方法,就会打开一个新的元素节点。这样就可以编写属性节点,利用WriteStartElement()方法进行注释,用WriteString()方法写字符串,或者用WriteStartElement()方法来写更多的子元素节点。 调用了WriteStartElement()方法打开的元素节点要通过调用WriteEndElement()方法关闭。 还可以通过调用WriteFullEndElement()方法,不管元素节点包不包含内容,该方法都会写出完整的结束元素标记符,关闭任何打开的元素节点。 看一个例子: private void button1_Click(object sender, EventArgs e) //Write XML Declaration //Write root element objXmlTW.WriteEndElement(); //End the XML Document //Write to the file and close XML文档如下: ③WriteStartAttribute()和writeEndAttribute()方法 WriteStartAttribute()有两个重载: ④ WriteElementString()和WriteAttributeString()方法 WriteElementString()方法可以编写一个包含字符串值的元素,它有两个重载形式: WriteAttributeString()方法有3个重载形式: WriteStartElement()方法和WriteElementString()方法,两种编写元素内容的方法的比较。 WriteStartE.ement("para"); 输出结果: <para> 在利用WriteElementString()方法时,只能编写文本节点,这是一种把元素写入输出结果中的简捷方法。 WriteStartAttribute()方法和WriteAttributeString()方法 WriteStartElement("para"); 输出结果:<para author="Do&0241;a&L.Perez"/> 添加一个属性的例子: private void button2_Click(object sender, EventArgs e) //Create document outline //Write XML Declaration //Write root element //Write attribute to the root element objXmlTW.WriteEndElement(); //End the XML Document //Write to the file and close textBox1.Text += ReadDocument(strFileName); ⑤WriteString()方法 WriteString()方法可以编写文本内容,这些内容在参数中指定是以字符串形式进行传递,当编写元素节点或者属性节点的内容时,此方法非常有用。该方法可以创建格式良好的XML。148 ⑥WriteNode()方法 WriteNode()方法可以从XmlReader实例中复制节点,并把它们写入XmlWrite流中——当希望从输入读取器中复制片断,并把它原封不动地写入到XmlWriter流中时,就可以使用它。 ⑦其它XmlWrite方法 ● Close() 方法:关闭任何打开的元素或者属性,最后关闭编写器和所与底层流,将WriteState属性设置为closed。 //Write the XML delcaration //add todays data //Write the close tag for the root elemant //Write the close tag for the root element 输出的XML文档: <?xml version="1.0"?> 在文档顶部定义today实体,使用WriteEntityRef()方法在文档中多次引用它。 2、XmlWrite 属性 ①WriteState 属性 WriteState 枚举值,该值可能是: ② XmlLang 属性 XmlLang 是一个只读属性,返回节点的xml:lang作用域,该作用域确定编写节点内容所使用的语言。xml:lang的值可以在节点上定义,也可以从父节点继承。如果没有定义语言,XmlLang属性将返回null。151 ③ XmlSpace 属性 三、XmlTextWriter 类 XmlTextWriter 派生于XmlWriter 抽象类。它为XmlWriter 抽象类添加了另外一些属性。 1、XmlTextWriter 构造函数 XmlTextWriter 类有三种构造函数。153 2、XmlTextWriter 属性 下面是XmlTextWriter 增加的额外属性: ①Formatting 属性 ②Indentation 属性 Indentation 属性设置节点缩进的字符数(用整数表示),它的默认值是2。只有当Formatting属性的值为Indented时才应用这种属性。 ③IndentChar 属性 当formatting 属性的值为Indented 时,IndentChar属性可以指定进行缩进的字符,它的默认只是空格符(0x20)。因此,所有锁紧操作的默认设置都是两个空格符(将indentation属性设置为2)。 ④Namespaces 属性 这个读/些属性可以确定编写器是否支持命名空间,它的默认值为 True。 ⑤QuoteChar 属性 这个读/些属性可以确定属性实用的引号,默认设置为双引号(0x34),还可以设置为单引号(0x39)。 ⑥BaseStream 属性 这是一个只读属性,返回XmlWriter 所使用的String 对象(如果没有底层的Stream对象其属性值为unll)。154 3、处理 XmlTextWriter private void button3_Click(object sender, EventArgs e) //Creates document outline //Sets the indentation for the XML file //Write XML Declaration //Write root element //Write attribute to the root element //Write a comment describing the document //Write contact child element node //Write empty title attribute //Wtrite <name> element node //Write<first>element node //Write<middle>element node //Write <last> element node //Write Full end of <name> element node //Write full end of <contact> element node objXmlTW.WriteEndElement(); //Ene the XML Document //Write to the file and close textBox1.Text += ReadDocument(strFileName); } private string ReadDocument(string strFileName) //Move to the first content node //read outer XML //close the reader return XmlString; |
利用.NET 类编写 XML 文档
最新推荐文章于 2024-05-30 16:57:45 发布