VB.NET XmlDocument 修改ChildNode value
时间: 2023-03-02 21:00:56 浏览: 62
可以使用以下代码来修改 VB.NET XmlDocument 中的 ChildNode 的值:
```
Dim doc As New XmlDocument()
doc.Load("file.xml")
Dim node As XmlNode = doc.SelectSingleNode("/root/child")
node.InnerText = "new value"
doc.Save("file.xml")
```
在上面的代码中,首先加载 XML 文件到 XmlDocument 对象中,然后使用 SelectSingleNode 方法来选择需要修改的 ChildNode,最后使用 InnerText 属性来修改它的值,最后使用 Save 方法保存修改后的 XML 文件。
相关问题
VB.NET XmlDocument 读取ChildNode value
VB.NET 中读取 XmlDocument 的 ChildNode 的值,可以使用如下代码:
```
Dim xmlDoc As New XmlDocument()
xmlDoc.Load("xmlFile.xml")
Dim rootNode As XmlNode = xmlDoc.DocumentElement
For Each childNode As XmlNode In rootNode.ChildNodes
If childNode.NodeType = XmlNodeType.Element Then
Console.WriteLine(childNode.InnerText)
End If
Next
```
在这段代码中,我们先创建一个 XmlDocument 对象,然后通过 Load 方法加载 XML 文件;接下来通过 `DocumentElement` 获取根节点,再遍历所有的 ChildNode,判断它们的类型是否为 `XmlNodeType.Element`,如果是,就输出它们的 InnerText。
VB.NET XmlDocument 创建ChildNode
在VB.NET中,使用XmlDocument创建ChildNode是很简单的。下面是一个示例代码:
```
Dim doc As XmlDocument = New XmlDocument()
Dim root As XmlElement = doc.CreateElement("Root")
doc.AppendChild(root)
Dim child As XmlElement = doc.CreateElement("Child")
root.AppendChild(child)
child.InnerText = "这是ChildNode的内容"
```
在上面的代码中,我们先创建了一个XmlDocument,然后创建了一个根元素并将其附加到XmlDocument中。接下来,我们创建了一个ChildNode并将其附加到根元素中。最后,我们为ChildNode设置了InnerText属性。