原文地址:http://www.dingos.cn/index.php?topic=899.0
XmlWriter 和 XmlTextWriter类定义在“System.XML”命名空间中。
XmlTextWriter类继承自XmlWriter类, 表示一个提供快速非缓存的生成基于W3C可扩展标记语言(XML)1.0版本的写器。
在这篇文章中将显示如何使用XmlTextWriter类创建XML文档并将信息写入文档。
【引入命名空间】
由于Xml类被定义在System.XML命名空间中,做的第一件事是需要将System.XML命名空间引入项目。
using System.Xml;
【创建XML文档】
如果文件不存在XmlTextWriter类构造函数将创建一个XML文件。在这个例子中,在“C:/temp”下创建一个新的XML文件叫“xmltest.xml”。
注意:如果不想将文件写入XML文件指向将XML内容显示在控制台,在构造函数中将“Console.Out”作为参数传递。
【添加数据到文档】
WriteStartDocument方法开始一个新文档。在WriteStartElement和WriteEndElement对用于添加文档的新元素。WriteString写一个字符串到文档。
XmlTextWriter类继承自XmlWriter类, 表示一个提供快速非缓存的生成基于W3C可扩展标记语言(XML)1.0版本的写器。
在这篇文章中将显示如何使用XmlTextWriter类创建XML文档并将信息写入文档。
【引入命名空间】
由于Xml类被定义在System.XML命名空间中,做的第一件事是需要将System.XML命名空间引入项目。
using System.Xml;
【创建XML文档】
如果文件不存在XmlTextWriter类构造函数将创建一个XML文件。在这个例子中,在“C:/temp”下创建一个新的XML文件叫“xmltest.xml”。
XmlTextWriter writer = new XmlTextWriter("C://temp//xmltest.xml", null);
注意:如果不想将文件写入XML文件指向将XML内容显示在控制台,在构造函数中将“Console.Out”作为参数传递。
XmlTextWriter writer = new XmlTextWriter(Console.Out);
【添加数据到文档】
WriteStartDocument方法开始一个新文档。在WriteStartElement和WriteEndElement对用于添加文档的新元素。WriteString写一个字符串到文档。
writer.WriteStartDocument();
writer.WriteComment("Commentss: XmlWriter Test Program");
writer.WriteProcessingInstruction("Instruction","Person Record");
writer.WriteStartElement("p", "person", "urn:person");
writer.WriteStartElement("LastName","");
writer.WriteString("Chand");
writer.WriteEndElement();
writer.WriteElementInt16("age","", 25);
writer.WriteEndDocument();
writer.WriteProcessingInstruction("Instruction","Person Record");
writer.WriteStartElement("p", "person", "urn:person");
writer.WriteStartElement("LastName","");
writer.WriteString("Chand");
writer.WriteEndElement();
writer.WriteElementInt16("age","", 25);
writer.WriteEndDocument();
【示例代码】
namespace WriteToXML{
using System;
using System.Xml;
/// <summary>
/// Summary description for Class1.
/// </summary>
public class Class1{
public Class1(){ }
public static int Main(string[] args){
try{
// Creates an XML file is not exist
XmlTextWriter writer = new XmlTextWriter("C://temp//xmltest.xml", null);
// Starts a new document
writer.WriteStartDocument();
//Write comments
writer.WriteComment("Commentss: XmlWriter Test Program");
writer.WriteProcessingInstruction("Instruction","Person Record");
// Add elements to the file
writer.WriteStartElement("p", "person", "urn:person");
writer.WriteStartElement("LastName","");
writer.WriteString("Chand");
writer.WriteEndElement();
writer.WriteStartElement("FirstName","");
writer.WriteString("Mahesh");
writer.WriteEndElement();
writer.WriteElementInt16("age","", 25);
// Ends the document
writer.WriteEndDocument();
} catch (Exception e){
Console.WriteLine ("Exception: {0}", e.ToString());
}
return 0;
}
}
}
using System;
using System.Xml;
/// <summary>
/// Summary description for Class1.
/// </summary>
public class Class1{
public Class1(){ }
public static int Main(string[] args){
try{
// Creates an XML file is not exist
XmlTextWriter writer = new XmlTextWriter("C://temp//xmltest.xml", null);
// Starts a new document
writer.WriteStartDocument();
//Write comments
writer.WriteComment("Commentss: XmlWriter Test Program");
writer.WriteProcessingInstruction("Instruction","Person Record");
// Add elements to the file
writer.WriteStartElement("p", "person", "urn:person");
writer.WriteStartElement("LastName","");
writer.WriteString("Chand");
writer.WriteEndElement();
writer.WriteStartElement("FirstName","");
writer.WriteString("Mahesh");
writer.WriteEndElement();
writer.WriteElementInt16("age","", 25);
// Ends the document
writer.WriteEndDocument();
} catch (Exception e){
Console.WriteLine ("Exception: {0}", e.ToString());
}
return 0;
}
}
}