# VB 2008 Merging XML-files



## Cato.M (Jul 30, 2008)

Hi,
I am pretty new to the VB programming language, and all new to the forum, so please have that in consideration when replying 

I am working on a small program (Form) for the user to browse for *.xml-files, adding them to a Listbox and then merge all individual xml-files to one xml-file.

I have the function to browse and select files, adding them to the Listbox. I also have the function to merge two or more static xml-files.

What I need, is for the merge-function to use all the files added in the listbox to update dynamically as new files are added each time the program run. See my code below, and read comments to understand my question further on 


```
Public Class Form1
 
Dim TotalFile As String = ""
 
' Sub for Open File Dialog to browse and open multiple files
 
Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
 
Dim OpenFileDialog1 As New OpenFileDialog
 
OpenFileDialog1.Multiselect = True
OpenFileDialog1.InitialDirectory = "C:\"
OpenFileDialog1.Filter = "XML files (*.xml)|*.xml|All files (*.*)|*.*"
OpenFileDialog1.FilterIndex = 2
OpenFileDialog1.RestoreDirectory = True
 
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
 
'Build a string with all the filenames selected
 
For Each x As String In OpenFileDialog1.FileNames
TotalFile = TotalFile & x & Environment.NewLine
 
ListBox1.Items.Add(TotalFile)
Next
 
MessageBox.Show(TotalFile)
 
End If
End Sub
 
 
' Merge function
' I would like to make some kind of For-loop from this, saying
' For j until all files in listbox are processed, make a new
' MSXML2.DOMDocument60.
' In other words, I need to insert a counting variable to include
' all selected files to the code below.
 
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
 
Dim doc1 As MSXML2.DOMDocument60
Dim doc2 As MSXML2.DOMDocument60
 
Dim doc2Node As MSXML2.IXMLDOMNode
 
doc1 = New MSXML2.DOMDocument60
doc2 = New MSXML2.DOMDocument60
 
doc1.load("C:\temp\my_xml_file1.xml")
doc2.load("C:\temp\my_xml_file2.xml")
 
For Each doc2Node In doc2.documentElement.childNodes
doc1.documentElement.appendChild(doc2Node)
 
Next
 
MsgBox(doc1.xml)
 
doc1.save("C:\temp\merged.xml")
 
End Sub
 
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 
 
ListBox1.SelectedIndexChanged
 
End Sub
End Class
```
Let me know if something was not clear about my problem

Regards,
Cato


----------



## Cato.M (Jul 30, 2008)

I think I'm on the way of getting it right now,
except I receive a 'System.NullReferenceException' in my Button1_Click sub.

Please let me know if anyone have any suggestions 


```
Public Class Form1

    Dim TotalFile As String = ""

    ' Sub for Open File Dialog to open multiple files
    Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click

        Dim OpenFileDialog1 As New OpenFileDialog

        OpenFileDialog1.Multiselect = True
        OpenFileDialog1.InitialDirectory = "C:\Documents and Settings\cmau\My Documents\Visual Studio 2008\Projects\MERGING_TWO_FILES_workign\XML"
        OpenFileDialog1.Filter = "XML files (*.xml)|*.xml|All files (*.*)|*.*"
        OpenFileDialog1.FilterIndex = 2
        OpenFileDialog1.RestoreDirectory = True

        If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            'Build a string with all the filenames selected

            For Each x As String In OpenFileDialog1.FileNames
                TotalFile = TotalFile & x & Environment.NewLine
                ListBox1.Items.Add(TotalFile)

            Next
            MessageBox.Show(TotalFile)

        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim i As Integer

        Dim doc1 As MSXML2.DOMDocument60

        doc1 = New MSXML2.DOMDocument60
        doc1.load(ListBox1.Items.Item(0))

        For i = 1 To Me.ListBox1.Items.Count - 1 Step 1

            Dim doc(i) As MSXML2.DOMDocument60

            Dim docNode(i) As MSXML2.IXMLDOMNode

            doc(i) = New MSXML2.DOMDocument60

            doc(i).load(ListBox1.Items.Item(i))


            ' On the following line I receive the NullReferenceException:
            For Each docNode(i) In doc(i).documentElement.childNodes
                doc1.documentElement.appendChild(docNode(i))

            Next

        Next i
        doc1.save("C:\Documents and Settings\cmau\My Documents\Visual Studio 2008\Projects\MERGING_TWO_FILES_workign\merged.xml")
        'MsgBox(doc(i).xml)

    End Sub

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged

    End Sub
End Class
```


----------



## Chicon (Jul 29, 2004)

Hi Cato.M,

Welcome to TSG ! 

Are you sure that the method _doc(i).documentElement.childNodes_ doesn't return a null object at the end of the loop ?
It's the only logical reason you get a _NullReferenceException_.
You should use a _while_ loop to test the return value of _doc(i).documentElement.childNodes_.


----------



## Cato.M (Jul 30, 2008)

I thought I finally solved it, but I did not. But my problem lies within the Button2_Click-for loop,
at the point where I add files to the ListBox.

What I accomplished, was to add the first selected file from OpenFileDialog to the ListBox,
as many times as the number of files I selected in the OpenFileDialog.

This is what I did:

For Each fileName As String In OpenFileDialog1.FileNames
ListBox1.Items.Add(OpenFileDialog1.FileName)

In other words, my problem is:
How do I add several files from a OpenFileDialog to a ListBox?


----------



## Cato.M (Jul 30, 2008)

It's working! 100% 

Fully working code:


```
Imports System.IO

Public Class Form1

    Dim TotalFile As String = ""

    ' Sub for Open File Dialog to open multiple files
    Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click

        Dim OpenFileDialog1 As New OpenFileDialog

        OpenFileDialog1.Multiselect = True
        OpenFileDialog1.InitialDirectory = "C:\"
        OpenFileDialog1.Filter = "XML files (*.xml)|*.xml|All files (*.*)|*.*"
        OpenFileDialog1.FilterIndex = 2
        OpenFileDialog1.RestoreDirectory = True

        If OpenFileDialog1.ShowDialog(Me) = Windows.Forms.DialogResult.OK Then

            For Each sFile As String In OpenFileDialog1.FileNames

                Dim pos As Integer = sFile.LastIndexOf("\"c)
                Me.ListBox1.Items.Add(System.IO.Path.GetFullPath(sFile))

            Next

        End If

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim i As Integer

        Dim doc1 As MSXML2.DOMDocument60

        doc1 = New MSXML2.DOMDocument60
        doc1.load(ListBox1.Items.Item(0))

        For i = 1 To Me.ListBox1.Items.Count - 1 Step 1

            Dim doc(i) As MSXML2.DOMDocument60

            Dim docNode(i) As MSXML2.IXMLDOMNode

            doc(i) = New MSXML2.DOMDocument60

            doc(i).load(ListBox1.Items.Item(i))

            For Each docNode(i) In doc(i).documentElement.childNodes
                doc1.documentElement.appendChild(docNode(i))

            Next

        Next i
        doc1.save("C:\temp\merged.xml")

    End Sub

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged

    End Sub
End Class
```


----------



## Chicon (Jul 29, 2004)

:up: Good job !


----------

