如何以编程方式在 xml 配置文件中的某些位置添加节点 [英] How to programatically add nodes at certain locations in xml config file
本文介绍了如何以编程方式在 xml 配置文件中的某些位置添加节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我们在 50 台客户端 PC 上安装了我们的软件.
We have our software installed on 50 client PCs.
软件从 xml 配置文件中选取值.每个客户端在配置文件中都有自己的个人节点值(真/假).
The software picks values from xml config file. Each client has his own personal node values (true/false) in config file.
现在我们发布了一个新版本的软件,在 xml 配置文件中增加了几个节点.
Now we are releasing a new version of software with few more nodes in the xml config file.
我们如何将新节点添加到客户端现有的配置文件,同时保留他的节点值(真/假).
How do we add new nodes to clients existing config files while retaining his node values (true/false).
注意我们必须向客户端提供脚本才能做到这一点,不能手动完成!
示例 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>
推荐答案
这是您可以开始使用的基本代码.
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
这篇关于如何以编程方式在 xml 配置文件中的某些位置添加节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文