2017-03-07 51 views
0

我想使用此代碼來更新我的XML「ini文件」:如何更新XML?

Sub WriteXML(strFilePathName As String, strNode As String, strNodeText As String) 
    Dim xmlDoc As New XmlDocument() 

    xmlDoc.Load(strFilePathName) 
    Dim MyXMLNode As XmlNode = xmlDoc.SelectSingleNode(strNode) 
    MyXMLNode.InnerText = "stuff" 
    xmlDoc.Save(strFilePathName) 
End Sub 

文件日期被修改,但進入「服務器名稱」不會改變。

有人可以建議我需要調整此xml的代碼?

<?xml version="1.0" encoding="utf-8"?> 
<!--XML Database.--> 
<DatabaseValues> 
    <ServerName>WS1</ServerName> 
    <DatabaseName>LGRVPark</DatabaseName> 
    <LastDatabaseServerChosen>Cellar</LastDatabaseServerChosen> 
</DatabaseValues> 
+0

不確定你到底在問什麼,但如果你正在嘗試創建xml,這個鏈接會告訴你如何去做。 https://www.dotnetperls.com/xmlwriter-vbnet – codeMonger123

+0

@ codeMonger123:他問爲什麼打開現有的XML文件並修改''的值在他再次保存文件時沒有得到體現。 –

+2

布魯斯,文件的完整路徑是什麼?確保你有寫權限。 –

回答

0

您可以使用XML Descendant Axis Property代替:

Dim x = XDocument.Load(strFilePathName) ' Imports System.Xml.Linq 
x...<DatabaseName>.Value = "stuff"  ' or x.Descendants("DatabaseName").Value = "stuff" 
x.Save(strFilePathName) 
0

謝謝大家對你的時間和建議。這是我創建的:

Public Function ReadXML(ByVal strFilePathName As String, ByVal strElement As String) As String 
    Dim xmlDoc As New XmlDocument() 
    Dim e As New clsEncryption 

    xmlDoc.Load(strFilePathName) 


    For Each element As XmlElement In xmlDoc.DocumentElement 
     If element.Name.Equals(strElement) Then 
      Return e.DecryptString(EncryptionKey, element.FirstChild.InnerText) 
     End If 
    Next 


End Function 

Public Sub WriteXML(ByVal strFilePathName As String, ByVal strElement As String, ByVal strElementText As String) 
    Dim xmlDoc As New XmlDocument() 
    Dim e As New clsEncryption 

    xmlDoc.Load(strFilePathName) 


    For Each element As XmlElement In xmlDoc.DocumentElement 
     If element.Name.Equals(strElement) Then 
      'element.InnerText = "inner text" <-- This works too. 
      element.FirstChild.InnerText = e.EncryptString(EncryptionKey, strElementText) 
     End If 
    Next 

    xmlDoc.Save(strFilePathName) 

End Sub