比较XML文件夹数据的最佳方法

时间:2013-09-05 15:30:41

标签: xml vb.net

我们正在将服务器环境从一个网络迁移到另一个网络。网络是完全分开的,无法看到对方。我正在编写一个程序,将我们当前生产文件服务器上的文件与未来生产文件服务器上的文件进行比较。

该计划需要列出以下内容:

  • 未来服务器中当前生产服务器中缺少的文件
  • 来自我们的生产服务器的未来服务器中过期的文件

我能想到的最快的方法是创建一个程序,浏览每个文件夹并创建一个对象来保存所有文件和文件夹。然后,我接受了该结构并将其序列化为XML格式。最终结果是我将有两个文件包含每个服务器上的所有文件和文件夹。

我现在的问题是我需要一种简单的方法来比较这两个文件,看看是否有任何差异。我想到的方法是将当前生产XML文件反序列化回对象,并遍历每个文件/文件夹检查以查看文件是否存在于未来的生产服务器上。

除了手动循环遍历每个文件外,是否有更简单的方法来比较两个XML文件以查看哪些对象不同?

以下是我用来生成两个文件的代码:

Imports System.IO

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim objFolder As New Folder
        objFolder = GetFolder("FOLDER TO BROWSE")

        Dim strObjects As String = SerializeObject(objFolder)
        With New StreamWriter("Out Path")
            .Write(strObjects)
        End With

    End Sub


    Function GetFolder(ByVal strPath As String) As Folder
        Dim objFolder As New Folder
        For Each File In New DirectoryInfo(strPath).GetFiles
            Dim oFile As New File
            oFile.Name = File.Name
            oFile.FullName = File.FullName
            oFile.DateCreated = File.CreationTime
            oFile.DateModified = File.LastWriteTime
            objFolder.lstFiles.Add(oFile)
        Next

        For Each Folder In New DirectoryInfo(strPath).GetDirectories
            objFolder.lstFolders.Add(GetFolder(Folder.FullName))
        Next

        Return objFolder
    End Function

    Public Shared Function SerializeObject(ByVal objToSerialize As Object) As String
        Dim objXML As New Xml.Serialization.XmlSerializer(objToSerialize.GetType) 
        Dim sw As New IO.StringWriter() 
        objXML.Serialize(sw, objToSerialize) 
        Return sw.ToString() 
    End Function

    Public Shared Function DeserializeObject(ByVal strSerializedObject As String, ByVal objType As Type) As Object
        Dim objXML As New Xml.Serialization.XmlSerializer(objType) 
        Dim sr As New IO.StringReader(strSerializedObject)
        Return CType(objXML.Deserialize(sr), Object) 
    End Function

End Class

<Serializable(), Xml.Serialization.XmlInclude(GetType(File))> _
Public Class Folder
    Public lstFiles As New List(Of File)
    Public lstFolders As New List(Of Folder)
End Class

<Serializable()> _
Public Class File
    Public Name As String
    Public FullName As String
    Public DateCreated As String
    Public DateModified As String
End Class

1 个答案:

答案 0 :(得分:1)

我最后通过使用上述方法解决了这个问题。我简单地反序列化了对象并循环遍历每个文件,检查目标文件是否存在并比较其他数据点。

以下是代码:

Imports System.IO 

Public Class Form1 

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
        Dim objFolder As New Folder 
        Dim strObjects As String = "" 

        With New StreamReader("INPUT FILE PATH") 
            strObjects = .ReadToEnd 
        End With 

        objFolder = DeserializeObject(strObjects, GetType(Folder)) 
        CheckFiles(objFolder) 

    End Sub 

    Sub CheckFiles(ByVal oFolder As Folder) 
        Dim FileName As String = "OUTPUT FILE PATH" 
        Dim strWrite As String = "" 
        For Each oFile In oFolder.lstFiles 
            If System.IO.File.Exists(oFile.FullName) Then 
                Dim fi As New FileInfo(oFile.FullName) 
                If fi.LastWriteTime <> oFile.DateModified Then 
                    strWrite += vbCrLf & oFile.Name & "," & oFile.FullName & ",File Out of Date," & oFile.DateModified 
                End If 
            Else 
                strWrite += vbCrLf & oFile.Name & "," & oFile.FullName & ",File Does Not Exist," & oFile.DateModified 
            End If 
        Next 
        Dim sw As New StreamWriter(FileName, True) 
        sw.Write(strWrite) 
        sw.Close() 
        sw.Dispose() 

        For Each oFolder2 In oFolder.lstFolders 
            CheckFiles(oFolder2) 
        Next 
    End Sub 

    Public Shared Function SerializeObject(ByVal objToSerialize As Object) As String 
        Dim objXML As New Xml.Serialization.XmlSerializer(objToSerialize.GetType) 
        Dim sw As New IO.StringWriter()
        objXML.Serialize(sw, objToSerialize)
        Return sw.ToString() 
    End Function 

    Public Shared Function DeserializeObject(ByVal strSerializedObject As String, ByVal objType As Type) As Object 
        Dim objXML As New Xml.Serialization.XmlSerializer(objType)
        Dim sr As New IO.StringReader(strSerializedObject) 
        Return CType(objXML.Deserialize(sr), Object) 
    End Function 

End Class 

<Serializable(), Xml.Serialization.XmlInclude(GetType(File))> _ 
Public Class Folder 
    Public lstFiles As New List(Of File) 
    Public lstFolders As New List(Of Folder) 
End Class 

<Serializable()> _ 
Public Class File 
    Public Name As String 
    Public FullName As String 
    Public DateCreated As String 
    Public DateModified As String 
End Class