寫xml文件的方法(vb.net)

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

 

 

 

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章