且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何以编程方式在 xml 配置文件中的某些位置添加节点

更新时间:2023-09-23 18:00:52

这是您可以开始使用的基本代码.

Here's the basic code you can start with.

Imports System.Xml

Public Class Form1
    Private Sub Test()
        Dim xDoc As XmlDocument
        Dim root As XmlNode
        Dim n As XmlNode

        xDoc = New XmlDocument()
        xDoc.Load("F:\tmp\a.xml")
        root = xDoc.SelectSingleNode("/ApplicationSettings")
        If xDoc.SelectSingleNode("/ApplicationSettings/NodeToBeAdded1") _
            Is Nothing Then
            n = root.InsertAfter(
                xDoc.CreateNode(XmlNodeType.Element, "NodeToBeAdded1", ""),
                xDoc.SelectSingleNode("/ApplicationSettings/Schema"))
            n.AppendChild(
                xDoc.CreateNode(XmlNodeType.Element, "XMLSubSomething", ""))
        End If
        xDoc.Save("F:\tmp\b.xml")
    End Sub
End Class