-
Aug 14th, 2012, 07:58 PM
#1
Thread Starter
New Member
[RESOLVED] [Newb Question] Merging two XML files (Visual Basic 2010 Express)
Evening,
First of all, I have very (VERY) limited knowledge of programming in the first place. That being said, for some statistical research I am trying to merge multiple .xml files to generate one database, but I'll start to try and get the code to merge just two of them at a time.
After looking up on how to merge two XML files, I found the following VB code (Step 8).
Now, I am using Visual Basic Express 2010 to generate a script; I created a form with a button and added the code linked above to the button. After following the steps in the link I changed the code from step 8 slightly since I am using XML Parser 6.0, and I use different locations :
Code:
Public Class Form1
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:\Users\XXXXX\Desktop\XXX\1000.xml")
doc2.Load("C:\Users\XXXXX\Desktop\XXX\1001.xml")
For Each doc2Node In doc2.documentElement.childNodes
doc1.documentElement.appendChild(doc2Node)
Next
MsgBox(doc1.xml)
doc1.save("C:\Users\XXXXX\Desktop\XXX\Merged DBS.xml")
End Sub
End Class
When I debug the code above, the following line of code returns an error:
Code:
For Each doc2Node In doc2.documentElement.childNodes
The error I receive is: 'NullReferenceException was unhandled' '(Object reference not set to an instance of an object.)'
(It appears to be referring to the .childNodes part)
Now for some questions:
- When I look at my link above, the initial code was meant for VBS 6.0; is this what causes the error?
- Is there any (easy) way to fix this error?
- Are there any viable (easier) alternatives to script the merging of multiple XML files?
Thanks in advance!
-
Aug 14th, 2012, 09:40 PM
#2
Member
Re: [Newb Question] Merging two XML files (Visual Basic 2010 Express)
Here is some code you can use.
Code:
Sub MergeXml(ByVal XmlPath1 As String, ByVal XmlPath2 As String)
' The XmlDocument that we want to merge from.
Dim xdFrom As New XmlDocument()
xdFrom.Load(XmlPath1)
' The XMLDocument that we want to merge to.
Dim xdTo As New XmlDocument()
xdTo.Load(XmlPath2)
' Loop through all of the nodes in the "from" document.
' We don't want to copy the root node in this instance.
For Each nod As XmlNode In xdFrom.DocumentElement.ChildNodes
' Import the node to our "to" document.
Dim tmpNod As XmlNode = xdTo.ImportNode(nod, True)
' Append this temporary node to the end of the "to" document
' but inside the root element.
xdTo.DocumentElement.AppendChild(tmpNod)
Next nod
' Save the "to" document with it's newly appended nodes.
xdTo.Save("C:\XmlTest.xml")
End Sub
Dont forget to add
-
Aug 15th, 2012, 08:51 AM
#3
Thread Starter
New Member
Re: [Newb Question] Merging two XML files (Visual Basic 2010 Express)
A thousand times thanks! It works!
One last question, what should I change to your code if I also want to copy the root node?
-
Aug 15th, 2012, 10:11 AM
#4
Member
Re: [Newb Question] Merging two XML files (Visual Basic 2010 Express)
Its not my code. I just turned it into a subroutine. Here is the tutorial I pulled it from.
http://www.vbforums.com/showthread.p...d-Delete-Nodes.
You can also merge two XML using datasets.
http://support.microsoft.com/kb/308066
Code:
Sub MergeXml(ByVal XmlPath1 As String, ByVal XmlPath2 As String)
'Documents we need to merge
Dim xmlreader1 As New XmlTextReader(XmlPath1)
Dim xmlreader2 As New XmlTextReader(XmlPath2)
'Create a new dataset to hold Xml1
Dim ds1 As New DataSet()
Try
'Load Xml into dataset
ds1.ReadXml(xmlreader1)
'Same for Xml2
Dim ds2 As New DataSet()
ds2.ReadXml(xmlreader2)
'Merge the two Xmls
ds1.Merge(ds2)
ds1.WriteXml("C:\XmlTest.xml", XmlWriteMode.IgnoreSchema)
Catch ex As Exception
MessageBox.Show("Error merging Xml Documents")
End Try
End Sub
-
Aug 15th, 2012, 08:50 PM
#5
Thread Starter
New Member
Re: [Newb Question] Merging two XML files (Visual Basic 2010 Express)
Well thanks anyway! Used quite some code from the examples you linked now. Finished the code today, so I'll mark this topic as solved.
Should anyone ever need .vb code to merge any finite number of .xml documents, given that their names are the numbers of a (not random) sequence, I will post this for reference:
Code:
Imports System.Xml
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
' From http://www.vbforums.com/
' Initialize.
Dim i = InputBox("Define the number of the first XML document.", "Starting Number?", "1000")
Dim a = InputBox("Define the number of the last XML document.", "Final Number?", "1009")
Dim b = Math.Abs(a - i) + 1
Dim cr As String = Environment.NewLine
Dim xdFrom As New XmlDocument()
Dim xdTo As New XmlDocument()
' Create New DBS;
' Start Writing.
Dim settings As New XmlWriterSettings()
settings.Indent = True
Dim XmlWrt As XmlWriter = XmlWriter.Create("C:\Users\XXXXX\Desktop\API\DBS\mergeddbs.xml", settings)
With XmlWrt
' Write the Xml declaration.
.WriteStartDocument()
' Write a comment.
.WriteComment("Merged Results.")
' Write the root element.
.WriteStartElement("DBS")
' Wrap it up.
.WriteEndElement()
.WriteEndDocument()
.Close()
End With
' Loop: Merge all documents.
For n = 1 To b
' Define the new last childNode. (Array <i>)
Dim Match As String = _
"<Match ID='" & i & "'>" & cr & _
"</Match>"
' The XmlDocument that we want to merge from. (Array <i>)
xdFrom.Load("C:\Users\XXXXX\Desktop\API\Download\" & i & ".xml")
' The XMLDocument that we want to merge to.
xdTo.Load("C:\Users\XXXXX\Desktop\API\DBS\mergeddbs.xml")
' Create a new XmlDocumentFragment for our document. This will be the last childNode.
Dim root As XmlNode = xdTo.DocumentElement
Dim docFrag As XmlDocumentFragment = xdTo.CreateDocumentFragment()
docFrag.InnerXml = Match
' Append the new childNode
root.AppendChild(docFrag)
' Loop through all of the nodes in the "from" document.
' We don't want to copy the root node in this instance.
For Each nod As XmlElement In xdFrom.DocumentElement
' Import the node to our "to" document.
Dim tmpNod As XmlElement = xdTo.ImportNode(nod, True)
' Finally append the clone to the new childNode.
' Remove .Lastchild if you want to append directly
' to the root of your XML document.
xdTo.DocumentElement.LastChild.AppendChild(tmpNod)
Next nod
' Save the "to" document with it's newly appended nodes.
xdTo.Save("C:\Users\XXXXX\Desktop\API\DBS\MergedDBS.xml")
' Prepare the next instance of the loop.
i = i + 1
Next
' Message if completed.
MsgBox("Merged " & b & " XML documents.")
' Close the application.
Close()
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|