Click here to Skip to main content
15,886,729 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have 2 xmls namely 1.xml and 2.xml.
i want to merge the 2.xml to 1.xml and create a single xml .

<Container>
  <Cow1>
    <IEnumMetadata />
  </Cow1>
  </Container>


and

<Container>
  <Cow2>
    <IEnumMetadata />
  </Cow2>
  <Cow3>
    <IEnumMetadata />
  </Cow3>
</Container>


i have merged them but the problem is i need to merge the content only for me the output is

<Container>
  <Cow1>
    <IEnumMetadata />
  </Cow1>
  <Container>
    <Cow2>
      <IEnumMetadata />
    </Cow2>
    <Cow3>
      <IEnumMetadata />
    </Cow3>
  </Container>
</Container>



but i want to get it as

<Container>
  <Cow1>
    <IEnumMetadata />
  </Cow1>
      <Cow2>
      <IEnumMetadata />
    </Cow2>
    <Cow3>
      <IEnumMetadata />
    </Cow3>
 </Container>


Any help please

What I have tried:

Dim doc1 As New XmlDocument()
       Dim doc2 As New XmlDocument()

       If File.Exists("2.xml") Then

           doc2.Load(2.xml)
       End If

       If File.Exists(1.xml) Then

           doc1.Load(1.xml)
       End If

       Dim lnodContainer As Xml.XmlNode = doc2.SelectSingleNode("//Container")

       Dim copiedNode As XmlNode = doc1.ImportNode(lnodContainer, True)
       doc1.DocumentElement.AppendChild(copiedNode)

       doc1.Save("Mergd.xml")
Posted
Updated 7-May-20 4:30am
v4

Try
VB.NET
Dim lnodContainer As Xml.XmlNode = doc2.SelectSingleNode("//Container")
ForEach child As XmlNode In lnodContainer.ChildNodes
   doc1.AppendChild(child)
Next

There are some missing double-quotes around file names; I would be surprised this would compile as is.
 
Share this answer
 
v2
Comments
Vert12 7-May-20 10:15am    
thanks but will the compiler accepts it because innerXml is a sting type, will it be converted to XmlNode type
phil.o 7-May-20 10:28am    
You are right. I modified my solution to enumerate the nodes in doc2 and append them to doc1.
Try something like this:
VB.NET
If File.Exists("1.xml") AndAlso File.Exists("2.xml") Then
    Dim doc1 As New XmlDocument()
    doc1.Load("1.xml")
    
    Dim doc2 As New XmlDocument()
    doc2.Load("2.xml")
    
    If doc2.DocumentElement.HasChildNodes Then
        For Each node As XmlNode In doc.DocumentElement.ChildNodes
            Dim copiedNode As XmlNode = doc1.ImportNode(node, True)
            doc1.DocumentElement.AppendChild(copiedNode)
        Next
    End If
    
    doc1.Save("Merged.xml")
End If
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900