我正在尝试更新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>
不确定我做错了什么,但我似乎无法在网上找到有关此问题的任何信息。想知道是否有人可以帮助我。任何帮助将不胜感激。
由于
答案 0 :(得分: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