如何关闭XML文档的实例

时间:2015-05-27 00:48:04

标签: vb.net xmldocument

尝试在XML文档上发出XVEDocument类的SAVE时,特别是在这行代码上:

myXmlDocument.Save(strFileZillaFilePathAndName)。

我得到...在条目更新期间 - 保存FileZilla文件失败。该进程无法访问文件&#C; \ Program Files(x86)\ FileZilla Server \ FileZilla Server.xml'因为它正被另一个进程使用。

它发生在从UpdateFtpAccountsProcess(...)函数中调用的FindAndUpdateXMLEntries(...)方法中。

我认为使用SAVE并不需要关闭。

它发生在函数的第二次执行时,因此似乎文件没有被关闭。它应该在第一次执行时关闭,并且不被另一个进程使用

    Public Function UpdateFtpAccountsProcess(ByVal strIpAddress As String, ByVal strPassword As String, ByVal iBackupSpaceId As Integer, ByVal iFtpAccountId As Integer, ByVal strFtpAccountPassword As String, ByVal strAccountType As String) As Boolean Implements IBackupServerService.UpdateFtpAccountsProcess

        Dim strErrorMessage As String = ""

        ' Start the backup service logging.
        StartTheLog(iBackupSpaceId)

        ' Write an log entry.
        strInputLogMessage = "In UpdateFtpAccountsProcess method."
        LogAnEntry(strInputLogMessage)

        ' Validate the password arguement in order to use this WCF service method.
        ' Read the app.config files "appSettings" section to get the backup server password. If it matches, continue.
        strEncryptedBackupServerPassword = GetAppSetting(strKeyPassword)

        If strEncryptedBackupServerPassword = strPassword Then
            ' Update the FTP account's encrypted password by "backup space id" and "account type". The accounts are stored in the FileZilla Server.xml file.

            ' Read the app.config file to get the “FileZilla path and name”.
            strFileZillaFilePathAndName = GetAppSetting(strKeyFileZilla)

            ' Read the app.config file to get the “FileZilla Application path and name”.
            strFileZillaFileApplicationPathAndName = GetAppSetting(strKeyFileZillaApplication)

            If ((strFileZillaFilePathAndName = "Not Found" Or strFileZillaFilePathAndName = "Error") Or (strFileZillaFileApplicationPathAndName = "Not Found" Or strFileZillaFileApplicationPathAndName = "Error")) Then
                strErrorMessage = "During update of FTP accounts - cannot find the FileZilla entries in the app config. Space Id: " & iBackupSpaceId.ToString()
            Else
                Try
                    ' Read the XML file and update the "password" of 1 of the 2 User entries [ftp accounts] - for the "backup space id". 
                    FindAndUpdateXMLEntries(strFileZillaFilePathAndName, iBackupSpaceId, strFtpAccountPassword, strAccountType)
                    Try
                        ' Execute command to run the FileZilla application to have FileZilla re-read the configuration.
                        ReloadFileZillaConfig(strFileZillaFileApplicationPathAndName)
                    Catch ex As Exception
                        strErrorMessage = "From: ReloadFileZillaConfig() ---> " & ex.Message
                    End Try
                Catch ex As Exception
                    ' Attach the message coming from the FindAndUpdateXMLEntries() method.
                    strErrorMessage = "During update of FTP account - Space Id: " & iBackupSpaceId.ToString() & " from: FindAndUpdateXMLEntries() ---> " & ex.Message
                End Try
            End If
        Else
            strErrorMessage = "During update of FTP accounts - invalid backup server password. It must match the app.config entry. Space Id: " & iBackupSpaceId.ToString()
        End If

        If strErrorMessage <> "" Then
            Throw New System.Exception(strErrorMessage)
        End If

        ' End the backup service logging.
        EndTheLog()

        Return bSuccess = True
    End Function

    Public Sub FindAndUpdateXMLEntries(ByVal strFileZillaFilePathAndName As String, ByVal iBackupSpaceId As Integer, ByVal strFtpAccountPassword As String, ByVal strAccountType As String)

        Dim strErrorMessage As String = ""

        Dim xmlNode1 As XmlNode
        Dim xmlNode2 As XmlNode
        Dim xmlNode3 As XmlNode
        Dim strUserNodeAtrributeNameValue As String
        Dim strUnderUserNodeThisNodesAtrributeNameValue As String
        Dim bSuccess As Boolean = False

        ' The XmlDocument class represents the XML document and has a Load method to load the document from a file, stream, or an XmlReader.
        ' So load in the XML file.
        Dim myXmlDocument As XmlDocument = New XmlDocument()

        ' Write an log entry.
        strInputLogMessage = "In FindAndUpdateXMLEntries method."
        LogAnEntry(strInputLogMessage)

        Try
            ' Load in the XML file.
            myXmlDocument.Load(strFileZillaFilePathAndName)

            ' Use the XmlNode object that the DocumentElement property of the XmlDocument returns to manipulate an XML node.
            xmlNode1 = myXmlDocument.DocumentElement

            ' Field to match to under the "Users" node. Building the "#-Private" or "#-Private" User root node.
            strUserNodeAtrributeNameValue = iBackupSpaceId.ToString() & "-" & strAccountType.Trim()

            ' The next field to match to under the "User" node that I find.
            strUnderUserNodeThisNodesAtrributeNameValue = "Pass"

            ' Iterate thru to find the node to update.
            For Each xmlNode1 In xmlNode1.ChildNodes
                If xmlNode1.Name = "Users" Then
                    ' Find the child nodes in "Users" only.
                    For Each xmlNode2 In xmlNode1.ChildNodes
                        ' Find the "User" node that I want.
                        If xmlNode2.Attributes("Name").Value = strUserNodeAtrributeNameValue Then
                            For Each xmlNode3 In xmlNode2.ChildNodes
                                ' Find the node that I want that is under the "User" node. Looking for: <Option Name="Pass">some value</Option>
                                If xmlNode3.Attributes("Name").Value = strUnderUserNodeThisNodesAtrributeNameValue Then
                                    ' Update the XML to the new password value.
                                    xmlNode3.InnerText = strFtpAccountPassword
                                    bSuccess = True
                                    Exit For
                                End If

                                If bSuccess = True Then
                                    Exit For
                                End If
                            Next
                        End If
                    Next
                End If

                If bSuccess = True Then
                    Exit For
                End If
            Next

            If bSuccess = True Then
                Try
                    ' Use the Save method of the XmlDocument class to save the altered XML back to the input XML file.
                    myXmlDocument.Save(strFileZillaFilePathAndName)
                Catch ex As Exception
                    strErrorMessage = "During entry update - save of FileZilla file failed. " & ex.Message
                End Try
            Else
                strErrorMessage = "During entry update - No User node to update"
            End If
        Catch ex As Exception
            strErrorMessage = "During entry update - load of FileZilla file failed. " & ex.Message
        End Try

        If strErrorMessage <> "" Then
            Throw New System.Exception(strErrorMessage)
        End If
    End Sub

0 个答案:

没有答案