0

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

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

现在我们正在发布一个新版本的软件,在 xml 配置文件中包含更多节点。

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

笔记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>
4

2 回答 2

2

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

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
于 2013-08-16T11:06:50.480 回答
0
Option Explicit
Option Strict

Imports System
Imports System.IO
Imports System.Xml

Public Class Sample

Public Shared Sub Main()

    Dim doc As New XmlDocument()
    doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" & _
                "<title>Pride And Prejudice</title>" & _
                "</book>")

    Dim root As XmlNode = doc.DocumentElement

    'Create a new node. 
    Dim elem As XmlElement = doc.CreateElement("price")
    elem.InnerText = "19.95" 

    'Add the node to the document.
    root.AppendChild(elem)

    Console.WriteLine("Display the modified XML...")
    doc.Save(Console.Out)
 End Sub 'Main 
 End Class 'Sample

http://msdn.microsoft.com/en-us/library/system.xml.xmlnode.appendchild.aspx

于 2013-08-16T10:59:52.840 回答