0

我正在尝试更新 XML 文档并替换单个节点的内容,但是,在我保存 XML 文件后,我的格式完全偏斜。

这是我的代码:

Public Sub UpdateSettingsXML(ByRef ElementName As String, ByRef ElementValue As String)
    Dim MyXML As New XmlDocument()
    Dim MyXMLNode As XmlNode

    MyXML.Load(SettingsXMLFile)
    MyXMLNode = MyXML.SelectSingleNode("//Data/Settings/" & ElementName)
    If Not MyXMLNode Is Nothing Then
        MyXMLNode.InnerText = ElementValue.ToString
        MyXML.Save(SettingsXMLFile)
    End If

    MyXMLNode = Nothing
    MyXML = Nothing
End Sub

我的原始 XML 文件(在更新节点之前)如下所示:

<?xml version="1.0" encoding="utf-8"?> 
<Data>   
 <Settings>
    <DropBoxLocation></DropBoxLocation>
    <ServerName></ServerName>
    <DatabaseName></DatabaseName>
    <Username></Username>
    <Password></Password>
    <OnlineFilename></OnlineFilename>
    <OfflineFilename></OfflineFilename>
    <EftposFilename></EftposFilename>
    <BankFilename></BankFilename>
    <POSFilename></POSFilename>
    <ProcessesFilename></ProcessesFilename>
    <FileReadDelay></FileReadDelay>   
 </Settings> 
</Data>

但是,一旦我保存 XML 文件,我的格式将如下所示:

<?xml version="1.0" encoding="utf-8"?> 
<Data>   
 <Settings>
    <DropBoxLocation>C:\Dropbox</DropBoxLocation>
    <ServerName>
    </ServerName>
    <DatabaseName>
    </DatabaseName>
    <Username>
    </Username>
    <Password>
    </Password>
    <OnlineFilename>
    </OnlineFilename>
    <OfflineFilename>
    </OfflineFilename>
    <EftposFilename>
    </EftposFilename>
    <BankFilename>
    </BankFilename>
    <POSFilename>
    </POSFilename>
    <ProcessesFilename>
    </ProcessesFilename>
    <FileReadDelay>
    </FileReadDelay>   
 </Settings> 
</Data>

不知道我做错了什么,但我似乎无法在网上找到有关此问题的任何信息。想知道是否有人可以帮助我。任何帮助,将不胜感激。

谢谢

4

1 回答 1

0

当您使用下面的代码时会发生什么?它使用 PRESERVEWHITE 参数

它可以修复您的格式吗?

Public Sub UpdateSettingsXML(ByRef ElementName As String, ByRef ElementValue As String)

  Dim MyXML As New XmlDocument()
  Dim MyXMLNode As XmlNode

  'REM - THIS LINE ADDED
  MyXML.PreserveWhitespace = false

  MyXML.Load(SettingsXMLFile)
  MyXMLNode = MyXML.SelectSingleNode("//Data/Settings/" & ElementName)
  If Not MyXMLNode Is Nothing Then
    MyXMLNode.InnerText = ElementValue.ToString

    'REM - THIS LINE ADDED
    MyXML.PreserveWhitespace = false
    MyXML.Save(SettingsXMLFile)
  End If
  MyXMLNode = Nothing
  MyXML = Nothing
End Sub
于 2013-03-19T06:52:47.503 回答