VB生成xml

Dim text As XmlText
Dim doc As New XmlDocument

'加入XML的声明段落 
Dim node As XmlNode = doc.CreateXmlDeclaration("1.0", "UTF-8", "yes")
doc.AppendChild(node)

'加入一个根元素 
Dim ele1 As XmlElement = doc.CreateElement("", "bookstore", "")
text = doc.CreateTextNode("")
ele1.AppendChild(text)
doc.AppendChild(ele1)

'加入一个子元素()
Dim ele2 As XmlElement = doc.CreateElement("", "book", "")
text = doc.CreateTextNode("")
ele2.AppendChild(text)

'为子元素"book"增加两个属性 
ele2.SetAttribute("genre", "", "fantasy")
ele2.SetAttribute("ISBN", "2-3631-4")

doc.ChildNodes.Item(1).AppendChild(ele2)

'创建三个子元素的子元素()
Dim ele3 As XmlElement = doc.CreateElement("", "title", "")
text = doc.CreateTextNode("Oberon's Legacy")
ele3.AppendChild(text)
doc.ChildNodes.Item(1).AppendChild(ele2).AppendChild(ele3)

Dim ele4 As XmlElement = doc.CreateElement("", "author", "")
text = doc.CreateTextNode("Corets, Eva")
ele4.AppendChild(text)
doc.ChildNodes.Item(1).AppendChild(ele2).AppendChild(ele4)

Dim ele5 As XmlElement = doc.CreateElement("", "price", "")
text = doc.CreateTextNode("5.95")
ele5.AppendChild(text)
doc.ChildNodes.Item(1).AppendChild(ele2).AppendChild(ele5)

'保存
doc.Save("D:\app.config")

文件打出来是这样的

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<bookstore>
<book genre="fantasy" ISBN="2-3631-4">
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>5.95</price>
</book>
</bookstore>

posted on 2014-04-28 11:10  零℃弥合  阅读(940)  评论(0编辑  收藏  举报

导航