一:XmlWriter的形式
Dim myXmlSettings As New XmlWriterSettings myXmlSettings.Indent = True myXmlSettings.NewLineOnAttributes = True Using ProductWriter As XmlWriter = XmlWriter.Create("path\XMLFile2.xml", myXmlSettings) ProductWriter.WriteComment("product product ") ProductWriter.WriteStartElement("All") ProductWriter.WriteStartElement("Product") ProductWriter.WriteAttributeString("Id", "101") ProductWriter.WriteAttributeString("COunt", "10") ProductWriter.WriteElementString("P1", "P2") ProductWriter.WriteEndElement() ProductWriter.WriteStartElement("Product") ProductWriter.WriteAttributeString("Id", "102") ProductWriter.WriteAttributeString("COunt", "10") ProductWriter.WriteElementString("P1", "P2") ProductWriter.WriteEndElement() ProductWriter.WriteStartElement("Product") ProductWriter.WriteAttributeString("Id", "103") ProductWriter.WriteAttributeString("COunt", "10") ProductWriter.WriteElementString("P1", "P2") ProductWriter.WriteEndElement () ProductWriter.WriteEndElement() End Using
二:XmlDocument的形式
Dim Doc As New XmlDocument() Dim dec As XmlDeclaration = Doc.CreateXmlDeclaration("1.0", _ Nothing, Nothing) Doc.AppendChild(dec) Dim DocRoot As XmlElement = Doc.CreateElement("Orders") Doc.AppendChild(DocRoot) Dim Order As XmlNode = Doc.CreateElement("Order") newAtt = Doc.CreateAttribute("Quantity") newAtt.Value = "100" Order.Attributes.Append(newAtt) DocRoot.AppendChild(Order) Doc.Save("path\XMLFile2.xml")