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

2024-04-03

我们的软件安装在 50 台客户端 PC 上。

软件从 xml 配置文件中选取值。每个客户端在配置文件中都有自己的个人节点值(真/假)。

现在我们发布了新版本的软件,在 xml 配置文件中添加了更多节点。

我们如何将新节点添加到客户端现有的配置文件,同时保留其节点值(真/假)。

NOTE We have to provide script to client to do this cannot do manually!

XML 示例:

  <?xml version="1.0" encoding="utf-8"?>
<ApplicationSettings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <dbEngine>true</dbEngine> 
  <EnableAuditLogging>true</EnableAuditLogging> 
  <Schema>
    <FileNo>05</FileNo>
  </Schema> 
  <nodeToBeAdded1>
   <xml/>
   <xml/>
  </nodeToBeAdded1>
  <nodeToBeAdded2>
   <DefaultPath="c:\"/>
  </nodeToBeAdded2>
  <ExportTo>
    <ExportTo>
      <ID>0</ID>
       <Path>C:\</Path>
    </ExportTo>
  </ExportTo>
</ApplicationSettings>

这是您可以开始的基本代码。

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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐