Dim XmlWrite As System.Xml.XmlTextWriter = New System.Xml.XmlTextWriter("d:/book.xml",System.Text.Encoding.UTF8) XmlWrite.WriteStartDocument() '开始一个文档,写下图第一行 XmlWrite.WriteStartElement("bookstore")'开始一个元素,根元素 XmlWrite.WriteStartElement("book")'开始一个元素book XmlWrite.WriteAttributeString("name","大闹天宫")'元素属性name XmlWrite.WriteAttributeString("id","001")'元素属性id XmlWrite.WriteElementString("author","小罗")'写一个元素 XmlWrite.WriteEndElement()'关闭元素book XmlWrite.WriteEndElement()'关闭元素bookstore XmlWrite.WriteEndDocument()'文档结束 XmlWrite.Flush()'刷新 XmlWrite.Close()'关闭
生成的XML文档结构如下:
<?xml version="1.0" encoding="utf-8"> <bookstore> <book name="大闹天宫" id="001"> <author>小罗</author> </book> </bookstore>