merge two xml files with LINQ

Marie-Andree

Member
Joined
Sep 9, 2015
Messages
8
Programming Experience
Beginner
My name is Marie and I am trying to merge two xml document and have a combine result in a new xml document called output.xml. It seems when I am debugging, only the first xml document is combining to the output.xml.
Here is my first xml called Nutrition.xml:

HTML:
<?xml version = "1.0"?

<nutrition xmlns="http://www.deitel.com/nutrition">
<title>Nutrition facts</title>
<size>28 grams</size>
<calories>140 calories</calories>
<caloriesFat>60 calories</caloriesFat>
<fat>8 grams</fat>
<saturedFat>2 grams of Satured Fat</saturedFat>
<choloesterol>5 milligrams</choloesterol>
<sodium>110 milligrams</sodium>
<carbohydrates>15 grams</carbohydrates>
<fiber>2 grams</fiber>
<sugar>15 grams</sugar>
<protein>1 gram</protein>
<description>Nutrition facts for a package of Grandma White's  cookies</description>
</nutrition>

This is my secound xml document called nutrition2.xml:
HTML:
<?xml version="1.0"?>

<nutrition xmlns="http://www.deitel.com/nutrition2">
<vitaminA>2</vitaminA>
<vitaminC>10</vitaminC>
<vitaminE>5</vitaminE>
<zinc>8</zinc>
</nutrition>

Here is my vb.net code:

Imports <xmlns:old="http://www.deitel.com/nutrition">
Imports <xmlns:new="http://www.deitel.com/nutrition2">


Public Class NutritionLINQ


Private Sub convertButton_Click(ByVal sender As System.Object,
  ByVal e As System.EventArgs) Handles CombineButton.Click

    Try
        ' attempt to load files
        Dim newDocument = XDocument.Load("Nutrition.xml")
        Dim oldDocument = XDocument.Load("Nutrition2.xml")

        ' convert from old to new format
        oldDocument = Transform(oldDocument)

        ' combine documents and write to output file
        SaveFinalDocument(newDocument, oldDocument)

        ' tell user we succeeded
        MessageBox.Show("Documents successfully combined.")
    Catch ' catch everything
        ' inform the user that there was a problem combining
        MessageBox.Show("Files could not be combined.")
        MessageBox.Show("This was probably caused by an I/O error.")
    End Try
End Sub ' Main

Private Function Transform(ByVal document As XDocument) As XDocument

    Dim newDocumentRoot =
    <nutrition>
        <%= From nutrition In document.Root.Elements()
             Select
             <nutrition information=
                 <%= nutrition.<old:title>.Value %>
                 <%= nutrition.<old:size>.Value %>
                 <%= nutrition.<old:calories>.Value %>
                 <%= nutrition.<old:caloriesFat>.Value %>
                 <%= nutrition.<old:fat>.Value %>
                 <%= nutrition.<old:saturedFat>.Value %>
                 <%= nutrition.<old:choloesterol>.Value %>
                 <%= nutrition.<old:Sodium>.Value %>
                 <%= nutrition.<old:carbohydrates>.Value %>
                 <%= nutrition.<old:fiber>.Value %>
                 <%= nutrition.<old:sugar>.Value %>
                 <%= nutrition.<old:protein>.Value %>
                 <%= nutrition.<old:description>.Value %>

                 <%= nutrition.<new:vitaminA>.Value %>
                 <%= nutrition.<new:vitaminC>.Value %>
                 <%= nutrition.<new:vitaminE>.Value %>
                 <%= nutrition.<new:zinc>.Value %>

             />
        %>
    </nutrition>

    Return New XDocument(newDocumentRoot) 

End Function 

Private Sub SaveFinalDocument(ByVal document1 As XDocument,
   ByVal document2 As XDocument)

    ' create root element, and fill with the elements
    ' contained in the roots of both documents
    Dim root =
       <nutrition>
           <%= document1.Root.Elements() %>
           <%= document2.Root.Elements() %>
       </nutrition>

    Dim finalDocument As New XDocument(root) ' create new document
    finalDocument.Save("output.xml") ' save document to file
End Sub 


End Class
 
Last edited by a moderator:
Transform method doesn't make sense, document.Root.Elements will return you each child of <nutrition>, these children does not themselves have any children. Then if you just use nutrition.Value as attribute value then you lose any meaning of the xml structure (for example you get nutrition information="2", nutrition information="10" etc)
 
Ok,

I think I understand, thank you!

I changed the transform method to this and it worked.

Private Function Transform(ByVal document As XDocument) As XDocument

Dim newDocumentRoot =
<nutrition>
<%= From nutrition In document.Root.Elements()
Select nutrition %>
</nutrition>


Return New XDocument(newDocumentRoot)
End Function


:)
 
That makes Transform method obsolete. Now you get the same output if you don't call Transform at all.
 

Similar threads

Latest posts

Back
Top