private void WriteXML()
{
XmlTextWriter xw = new XmlTextWriter(@"D:/books.xml",null);
xw.WriteStartDocument();
xw.WriteStartElement("bookstore");
xw.WriteStartElement("book");
xw.WriteAttributeString("name","book1");
xw.WriteAttributeString("id","001");
xw.WriteElementString("author","author1");
xw.WriteElementString("price","32.00");
xw.WriteEndElement();
xw.WriteStartElement("book");
xw.WriteAttributeString("name","book2");
xw.WriteAttributeString("id","002");
xw.WriteElementString("author","author2");
xw.WriteElementString("price","32.00");
xw.WriteEndElement();
xw.WriteEndElement();
xw.WriteEndDocument();
xw.Flush();
xw.Close();
}
//输出结果
<?xml version="1.0"?>
<bookstore>
<book name="book1" id="001">
<author>author1</author>
<price>32.00</price>
</book>
<book name="book2" id="002">
<author>author2</author>
<price>32.00</price>
</book>
</bookstore>