VB.net xml文件操作实例
时间: 2023-09-11 21:10:35 浏览: 44
好的,下面是一个 VB.NET 操作 XML 文件的示例:
1. 创建 XML 文件:
```
Dim xmlDoc As New XmlDocument()
Dim xmlDeclaration As XmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", Nothing)
xmlDoc.AppendChild(xmlDeclaration)
Dim rootNode As XmlElement = xmlDoc.CreateElement("Root")
xmlDoc.AppendChild(rootNode)
xmlDoc.Save("test.xml")
```
2. 读取 XML 文件:
```
Dim xmlDoc As New XmlDocument()
xmlDoc.Load("test.xml")
Dim rootNode As XmlElement = xmlDoc.DocumentElement
For Each childNode As XmlNode In rootNode.ChildNodes
Console.WriteLine(childNode.Name & ": " & childNode.InnerText)
Next
```
3. 修改 XML 文件:
```
Dim xmlDoc As New XmlDocument()
xmlDoc.Load("test.xml")
Dim rootNode As XmlElement = xmlDoc.DocumentElement
For Each childNode As XmlNode In rootNode.ChildNodes
If childNode.Name = "NodeName" Then
childNode.InnerText = "New Value"
End If
Next
xmlDoc.Save("test.xml")
```
希望这些代码对你有所帮助。
阅读全文