---
title: "写xml文件的方法(vb.net)"
author: "newand"
date: "2008-05-09T17:31:22Z"
tags: [".net", "VB", "VB.NET", "XML"]
slug: "xml-vb-net"
draft: false
lastmod: "2008-05-09T17:31:22Z"
id: 191524
url: "https://newand.iteye.com/blog/191524"
---

一：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") 

```