Help with merging files

Hi,

I'm using the code below to merge file for multiple countries, how do I modify it to avoid duplicating the code for each country?
fsItem= New System.IO.FileStream(Application.StartupPath + "\LinkFiles\LinkFinalMain.xml", IO.FileMode.Open)
        dtsetItem.Clear()
        dtsetItem.ReadXml(fsItem)
        fsItem.Close()
        Dim DestinationPath As String = IO.Path.Combine(Application.StartupPath + "\LinkFiles\", IO.Path.GetFileName("\LinkFiles\LinkFinalBEL.xml"))

        If IO.File.Exists(DestinationPath) Then
            fsReceiver = New System.IO.FileStream(Application.StartupPath + "\LinkFiles\LinkFinalBEL.xml", IO.FileMode.Open)
            dtsetReceiver.Clear()
            dtsetReceiver.ReadXml(fsReceiver)
            dtsetItem.Merge(dtsetReceiver, True, MissingSchemaAction.AddWithKey)
        End If
        '  MsgBox(dtsetItem.Tables(0).Rows.Count)
        Dim DestinationPathA As String = IO.Path.Combine(Application.StartupPath + "\LinkFiles\", IO.Path.GetFileName("\LinkFiles\LinkFinalCAN.xml"))
        If IO.File.Exists(DestinationPathA) Then

            fsDonor = New System.IO.FileStream(Application.StartupPath + "\LinkFiles\LinkFinalCAN.xml", IO.FileMode.Open)
            dtsetDonor.Clear()
            dtsetDonor.ReadXml(fsDonor)
            fsDonor.Close()
            dtsetItem.Merge(dtsetDonor, True, MissingSchemaAction.AddWithKey)
        End If

        Dim DestinationPathB As String = IO.Path.Combine(Application.StartupPath + "\LinkFiles\", IO.Path.GetFileName("\LinkFiles\LinkFinalCZE.xml"))
        If IO.File.Exists(DestinationPathB) Then
            fsDonor = New System.IO.FileStream(Application.StartupPath + "\LinkFiles\LinkFinalCZE.xml", IO.FileMode.Open)
            dtsetDonor.Clear()
            dtsetDonor.ReadXml(fsDonor)
            fsDonor.Close()
            dtsetItem.Merge(dtsetDonor, True, MissingSchemaAction.AddWithKey)
        End If

Open in new window

vcharlesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ElrondCTCommented:
How about the following?

        fsItem = New System.IO.FileStream(Application.StartupPath + "\LinkFiles\LinkFinalMain.xml", IO.FileMode.Open)
        dtsetItem.Clear()
        dtsetItem.ReadXml(fsItem)
        fsItem.Close()
        Dim strCountries() As String = {"BEL", "CAN", "CZE"}
        For iCountry As Integer = 0 To 2
            Dim DestinationPath As String = IO.Path.Combine(Application.StartupPath + "\LinkFiles\", IO.Path.GetFileName("\LinkFiles\LinkFinal" & strCountries(iCountry) & ".xml"))

            If IO.File.Exists(DestinationPath) Then
                fsReceiver = New System.IO.FileStream(Application.StartupPath + "\LinkFiles\LinkFinal" & strCountries(iCountry) & ".xml", IO.FileMode.Open)
                dtsetReceiver.Clear()
                dtsetReceiver.ReadXml(fsReceiver)
                dtsetItem.Merge(dtsetReceiver, True, MissingSchemaAction.AddWithKey)
            End If
        Next iCountry

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ElrondCTCommented:
I should note that my code assumes there's no particular reason that one of the files is read into dtsetReceiver and the other two are read into dtsetDonor. If those are significant outside of the code snippet here, then you'll need to have a way to reference them by subscript, such as:
        Dim dt(2) as DataSet
        dt(0) = dtsetReceiver 
        dt(1) = dtsetDonor
        dt(2) = dtsetDonor

Open in new window

and then change all the references from dtsetReceiver to dt(iCountry).
0
vcharlesAuthor Commented:
Thank You, I will try it and get back to you.
0
vcharlesAuthor Commented:
Thank You.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.