I have two XML files, and I want to merge these two files into one. But how? I tried a lot, but nothing helps. As you can see, the merged XML left the text from the second attribute if it came from the first XML. Secondly, the Item must be ordered there Id / Name / anyname, which has the first attribute. Third, if node does not exist in XML 2, then it must be created in the same place as in XML 1.
The XML files presented here are just a fragment of the whole XML, there are much more attribute names.
How can I do this using C #?
XML 1
<APPLICATION> <AC> <CLASS Name="Hello1" Capt="do1"/> <CLASS Name="Hello2" Capt="do2"/> <CLASS Name="Hello5" Capt="do5"/> <CLASS Name="Hello8" Capt="do8"/> </AC> <BO> <ITEM Id="1" DefaultValue="name1"/> <ITEM Id="3" DefaultValue="name3"/> <ITEM Id="11" DefaultValue="name11"/> <ITEM Id="12" DefaultValue="name12"> <VAL> <REASON Id="Job1" SecondOne="Hallo"/> </VAL> </ITEM> </BO> <POP Id="Green" Value="Monster"/> <POP Id="Blue" Value="Doggie"/>
XML 2
<APPLICATION> <AC> <CLASS Name="Hello1" Capt="dodo1"/> <CLASS Name="Hello2" Capt="dodo2"/> <CLASS Name="Hello3" Capt="dodo3"/> <CLASS Name="Hello9" Capt="dodo9"/> </AC> <CARS Wheel="Fore" Default="45x255xZ"/> <CARS Wheel="BACK" Default="45x255xZ"/> <CARS Wheel="SPARE" Default="45x255xZ"/> <BO> <ITEM Id="1" DefaultValue="namename1"/> <ITEM Id="3" DefaultValue=""/> <ITEM Id="9" DefaultValue="name11"/> <ITEM Id="10" DefaultValue="name12"> <VAL> <REASON Id="Job1" SecondOne="Hallo"/> </VAL> </ITEM> </BO>
XML should look like this after merging:
<APPLICATION> <AC> <CLASS Name="Hello1" Capt="dodo1"/> <CLASS Name="Hello2" Capt="dodo2"/> <CLASS Name="Hello3" Capt="dodo3"/> <CLASS Name="Hello5" Capt=""/> <CLASS Name="Hello8" Capt=""/> <CLASS Name="Hello9" Capt="dodo9"/> </AC> <CARS Wheel="Fore" Default="45x255xZ"/> <CARS Wheel="BACK" Default="45x255xZ"/> <CARS Wheel="SPARE" Default="45x255xZ"/> <BO> <ITEM Id="1" DefaultValue="namename1"/> <ITEM Id="3" DefaultValue=""/> <ITEM Id="9" DefaultValue="name11"/> <ITEM Id="10" DefaultValue="name12"> <VAL> <REASON Id="Job1" SecondOne="Hallo"/> </VAL> </ITEM> <ITEM Id="11" DefaultValue=""/> <ITEM Id="12" DefaultValue=""> <VAL> <REASON Id="Job1" SecondOne=""/> </VAL> </ITEM> </BO> <POP Id="Green" Value=""/> <POP Id="Blue" Value=""/>
Thanx for all the answers, but still I have a problem that I donโt know how to tags, so I canโt hardcode the tags.
I just have to give you an example of how it might look. But the next time I get my XML files, the tags above can be completely different. That's the problem. Therefore, I canโt say the new XElement ("BO", boChildren), because next time this tag no longer exists.
Or I canโt hardcode this ==> var cars = xDocuments.SelectMany (x => x.Root.Elements ("CARS")). Merge (); because the next time I get my XML files, "CARS" no longer exists.