Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何以编程方式在xml配置文件中的特定位置添加节点_Xml_Vb.net - Fatal编程技术网

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

如何以编程方式在xml配置文件中的特定位置添加节点,xml,vb.net,Xml,Vb.net,我们的软件安装在50台客户端PC上 软件从xml配置文件中选取值。每个客户端在配置文件中都有自己的个人节点值(true/false) 现在,我们发布了一个新版本的软件,在xml配置文件中没有更多的节点 如何将新节点添加到客户端现有配置文件中,同时保留其节点值(true/false) 注意我们必须向客户端提供脚本才能执行此操作,但不能手动执行 示例XML: <?xml version="1.0" encoding="utf-8"?> <ApplicationSettings

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

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

现在,我们发布了一个新版本的软件,在xml配置文件中没有更多的节点

如何将新节点添加到客户端现有配置文件中,同时保留其节点值(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>

真的
真的
05
0
C:\
选项显式
选项严格
导入系统
导入System.IO
导入System.Xml
公共类样本
公共共享子主目录()
Dim doc作为新的XmlDocument()
doc.LoadXml(“&”_
《傲慢与偏见》及_
"")
Dim root作为XmlNode=doc.DocumentElement
'创建一个新节点。
Dim elem为xmlement=doc.CreateElement(“价格”)
elem.InnerText=“19.95”
'将节点添加到文档中。
root.AppendChild(元素)
WriteLine(“显示修改后的XML…”)
单据保存(控制台输出)
端接头“主
“最终类”示例

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

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(旧/新版本)示例吗?这听起来像是一个简单的任务(即打开配置文件,向其中添加新节点并保存)。如何以编程方式添加节点?我们必须向客户端提供脚本来完成此操作,但不能手动完成!如果你发布了MSDN的代码,你应该提到这一点
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