TM80
TM80

Reputation: 123

Merging xml when xml is in use? (vb.net)

I have the following code which I would like to use to merge two xml files into one xml, But I keep messing it up along the way. Firstly I would like to open B.xml and merge it with A.xml. Once merged I would like to save it as C:\A.xml again, however I can't get it to overwrite itself because it's in use. At the moment I can only save A.xml merged with B.xml if the file name is other than C:\A.xml. is there a way around this?

        'create a openfile dialog
    Dim open_file As New OpenFileDialog

    'give its filter
    open_file.Filter = "Files (*.xml) | *.xml"
    open_file.Title = "Merge a Library"
    open_file.FileName = "Select a Library..."
    open_file.InitialDirectory = "C:\"

    Try

        'if ok click at opendialog
        If open_file.ShowDialog() = DialogResult.OK Then

            Dim xmlreader1 As New XmlTextReader("C:\A.xml")

            Dim xmlreader2 As New XmlTextReader(open_file.FileName)

            Dim ds1 As New DataSet()
            Try
                ds1.ReadXml(xmlreader1)

                Dim ds2 As New DataSet()
                ds2.ReadXml(xmlreader2)

                ds1.Merge(ds2)
                ds1.WriteXml("C:\A.xml", XmlWriteMode.IgnoreSchema)
                Console.WriteLine("Completed merging XML documents")
            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try
            Console.Read()

        End If
    Catch ex As Exception

        'if any problem show error
        MsgBox(ex.Message)
    End Try

Upvotes: 2

Views: 59

Answers (1)

The xmlTextReader object you are using are holding open handles to the xml files. In order to free up the resources you would need to call the .Dispose() method on both XML readers (first example below). However I would suggest placing your XMLTextReader objects inside of a Using block which will automatically call the .Dispose() method for you and make for cleaner code.

Try
    'if ok click at opendialog
    If open_file.ShowDialog() = DialogResult.OK Then
        Dim xmlreader1 As New XmlTextReader("C:\A.xml")
        Dim xmlreader2 As New XmlTextReader(open_file.FileName)
        Dim ds1 As New DataSet()
        Try
            ds1.ReadXml(xmlreader1)

            Dim ds2 As New DataSet()
            ds2.ReadXml(xmlreader2)

            ds1.Merge(ds2)
            xmlreader1.Dispose()
            ds1.WriteXml("C:\A.xml", XmlWriteMode.IgnoreSchema)
            Console.WriteLine("Completed merging XML documents")
        Catch ex As Exception
            Console.WriteLine(ex.Message)
        Finally
            If xmlreader1.ReadState <> ReadState.Closed Then xmlreader1.Dispose()
            xmlreader2.Dispose()
        End Try
        Console.Read()
    End If
Catch ex As Exception
    'if any problem show error
    MsgBox(ex.Message)
End Try

    Try
        'if ok click at opendialog
        If open_file.ShowDialog() = DialogResult.OK Then
            Dim ds1 As New DataSet()
            Dim ds2 As New DataSet()
            Try
                Using xmlreader1 As New XmlTextReader("C:\A.xml")
                    ds1.ReadXml(xmlreader1)
                End Using

                Using xmlreader2 As New XmlTextReader(open_file.FileName)
                    ds2.ReadXml(xmlreader2)
                End Using

                ds1.Merge(ds2)

                ds1.WriteXml("C:\A.xml", XmlWriteMode.IgnoreSchema)
                Console.WriteLine("Completed merging XML documents")
            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try
            Console.Read()
        End If
    Catch ex As Exception
        'if any problem show error
        MsgBox(ex.Message)
    End Try

Upvotes: 1

Related Questions