Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
<root>
<purchase></purchase>
<sales></sales>
</root>
<root>


I want it like that because currently all these xmls are different and I want them to merge in one xml
XML
<root>
<purchase></purchase>
<sales></sales>
</root>

<root>
<purchase></purchase>
<sales></sales>
</root>

After merging all the xml's I want to create one csv file from that.

What I have tried:

For Each xml In csvXmlList
Dim dsTemp As New DataSet()
dsTemp.ReadXml(xml)
dsAll.Merge(dsTemp)
Next
Posted
Updated 24-Aug-16 0:58am
v2

Well, there's no simple way to merge xml documents. Why? You have to be completely sure that desired content will be OK (in aspect of structure and values).

Let me explain it on below example:
C#
//content of doc #1
string xcontent1 = @"<root>
<Item id='1'>
	<Subitems>
		<Subitem sid='1'>A</Subitem>
		<Subitem sid='2'>B</Subitem>
	</Subitems>
</Item>
<Item id='2'>
	<Subitems>
		<Subitem sid='1'>A</Subitem>
		<Subitem sid='2'>B</Subitem>
		<Subitem sid='3'>C</Subitem>
	</Subitems>
</Item>
</root>";
//content of doc #2
string xcontent2 = @"<root>
<Item id='1'>
	<Subitems>
		<Subitem sid='1'>A</Subitem>
		<Subitem sid='2'>E</Subitem>
		<Subitem sid='3'>F</Subitem>
	</Subitems>
</Item>
<Item id='3'>
	<Subitems>
		<Subitem sid='1'>A</Subitem>
		<Subitem sid='2'>B</Subitem>
		<Subitem sid='3'>C</Subitem>
	</Subitems>
</Item>
</root>";

//first document
XDocument xdoc1 = XDocument.Parse(xcontent1);
//second document
XDocument xdoc2 = XDocument.Parse(xcontent2);
//final document (merged)
XDocument xdst = new XDocument();
//add entire content of document #1
xdst.Add(xdoc1.Root);
//add content of document #2 without root
xdst.Root.Add(xdoc2.Root.Elements());


Result of merged document:
XML
<root>
  <Item id="1">
    <Subitems>
      <Subitem sid="1">A</Subitem>
      <Subitem sid="2">B</Subitem>
    </Subitems>
  </Item>
  <Item id="2">
    <Subitems>
      <Subitem sid="1">A</Subitem>
      <Subitem sid="2">B</Subitem>
      <Subitem sid="3">C</Subitem>
    </Subitems>
  </Item>
  <Item id="1">
    <Subitems>
      <Subitem sid="1">A</Subitem>
      <Subitem sid="2">E</Subitem>
      <Subitem sid="3">F</Subitem>
    </Subitems>
  </Item>
  <Item id="3">
    <Subitems>
      <Subitem sid="1">A</Subitem>
      <Subitem sid="2">B</Subitem>
      <Subitem sid="3">C</Subitem>
    </Subitems>
  </Item>
</root>


As you can see, merged xml document contains 2 Item nodes with the same id (1), but with different content (Subitems). There's one rule, which you have to respect: Programmers have to be wiser than users. ;)

Conclusion: You have to provide method which will go through the nodes collection (and their subnodes) to be able to compare each node with currently imported node.
 
Share this answer
 
Comments
g.bhangu 27-Jun-17 23:29pm    
hi this is giving an exception: Data at the root level is invalid. Line 1, position 1.plz help. i have the same issue,that i want to add the elements but root node should be same. no break in it.
Maciej Los 28-Jun-17 1:54am    
You mean, you want to second root element? You should avoid that, because you'll get incorrect xml data structure. Without ssing your xml i can't help. Please, do not post its content in comment. Do me and other members favour and post your question on Quick Answers board, using Ask link.
g.bhangu 28-Jun-17 2:10am    
i have solved that problem thanks. another issue is :
https://www.codeproject.com/Questions/1193862/Compare-and-display-the-element-value-from-XML-fil

plz check.
This is not an xml file
XML
<root>
    <purchase></purchase>
    <sales></sales>
</root>
<root></root>

But this is
XML
<root>
    <purchase></purchase>
    <sales></sales>
</root>


When you merge 2 xml files, you need to know that the root is unique in resulting file.
So at least the resulting file can look like
XML
<root>
    <purchase></purchase>
    <sales></sales>
</root>
<root></root>
<root></root>
    <purchase></purchase>
    <sales></sales>

<root></root>

As you can see merging 2 xml files is not just concatenating them.
 
Share this answer
 
I got the solution by digging some more into few places and it is merging multiple XML files amazingly as expected. I have used XElement in VB.NET to accomplish this.

Requirement:- import :-
VB.NET
Imports System.Xml.Linq


VB
Dim xdoc As XElement
Dim xdoc2 As XElement = <roots></roots>

VB.NET
For Each xml In XmlList
           xdoc = XElement.Parse(xml)
           xdoc2.Add(xdoc)

Next
 
Share this answer
 
v3
Comments
Maciej Los 24-Aug-16 6:58am    
Wrong approach! Please see my aswer.

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