Create XML using class XmlTextWriter
2008-12-11 11:30
453 查看
This is an example to create XML document using XmlTextWriter in C#.
The following XML is the result i want to generate.
<?xml version="1.0" encoding="gb2312"?>
<Books>
<Book genre="Mystery" publicationdate="2001" ISBN="123456789">
<title>The Case of the Missing Cookie</title>
<author Country="America">
<name>Cookie Monster</name>
</author>
<price>$9.99</price>
</Book>
</Books>
The C# code is as follows,
XmlTextWriter xtw = new XmlTextWriter("d://file.xml", Encoding.Default);
xtw.Formatting = Formatting.Indented;
xtw.WriteStartDocument();
xtw.WriteStartElement("Books");
// First Book
xtw.WriteStartElement("Book");
// write book attributes
xtw.WriteAttributeString("genre", "Mystery");
xtw.WriteAttributeString("publicationdate", "2001");
xtw.WriteAttributeString("ISBN", "123456789");
// write book elements
xtw.WriteElementString("title", "The Case of the Missing Cookie");
// write sub element
xtw.WriteStartElement("author");
xtw.WriteAttributeString("Country", "America");
xtw.WriteElementString("name", "Cookie Monster");
xtw.WriteEndElement();
xtw.WriteElementString("price", "$9.99");
xtw.WriteEndElement();
xtw.WriteEndElement();
xtw.WriteEndDocument();
// close writer
xtw.Close();
The following XML is the result i want to generate.
<?xml version="1.0" encoding="gb2312"?>
<Books>
<Book genre="Mystery" publicationdate="2001" ISBN="123456789">
<title>The Case of the Missing Cookie</title>
<author Country="America">
<name>Cookie Monster</name>
</author>
<price>$9.99</price>
</Book>
</Books>
The C# code is as follows,
XmlTextWriter xtw = new XmlTextWriter("d://file.xml", Encoding.Default);
xtw.Formatting = Formatting.Indented;
xtw.WriteStartDocument();
xtw.WriteStartElement("Books");
// First Book
xtw.WriteStartElement("Book");
// write book attributes
xtw.WriteAttributeString("genre", "Mystery");
xtw.WriteAttributeString("publicationdate", "2001");
xtw.WriteAttributeString("ISBN", "123456789");
// write book elements
xtw.WriteElementString("title", "The Case of the Missing Cookie");
// write sub element
xtw.WriteStartElement("author");
xtw.WriteAttributeString("Country", "America");
xtw.WriteElementString("name", "Cookie Monster");
xtw.WriteEndElement();
xtw.WriteElementString("price", "$9.99");
xtw.WriteEndElement();
xtw.WriteEndElement();
xtw.WriteEndDocument();
// close writer
xtw.Close();
相关文章推荐
- [C#] Format a XML string using XmlTextWriter
- Create XML using class XmlDocument
- (转)使用XmlTextWriter与XmlTextReader访问创建和读取XML文件
- JavaScript Xml Writer Class
- c#以文件流的形式输出xml(可以解决内存溢出)-XmlTextWriter
- [转载]关于XmlTextWriter的使用方法
- XmlTextWriter.WriteString与WriteRaw的区别
- 编写XML XmlTextWriter与XmlDocument
- 今天试了一下ajax dropdownlist联动,实际上是先写在一个XmlTextWriter(Response.OutputStream,然后再response回去(等同于一个XML),里面有很多要注意的地方,比如dropdownlist的清空,遍历X
- 我用xmltextwriter写了一个xml文件,为什么找不到该文件
- Home not found. Define system property "openfireHome" or create and add the openfire_init.xml file to the classpath
- C#中XmlTextWriter读写xml文件详细介绍
- C#中XmlTextWriter读写xml文件详细介绍(转)
- c#以文件流的形式输出xml(可以解决内存溢出)-XmlTextWriter
- Generate the java class from XML Schema file using JAXB
- 编写XML XmlTextWriter与XmlDocument(转)
- XmlTextWriter创建XML文件
- 用XmlTextWriter写出的XML文件没有换行,用记事本打开XML文件的时候都写在了一行上,请问怎样加入换行!
- RSS制作(C#) - 使用XML DOM和XmlTextWriter(见评论)两种方法
- 在XmlTextWriter中写入一个cdata