I need to 'merge' 2 xml files together but take into account elements that appear in both and merge attributes that are different in each. Here's an example of what I mean:
Input: Xml1
<?xml version="1.0"?>
<Style Width="1024" Height="768">
<BaseStyle Width="1024" Height="768" BackgroundPath="./Images/BackgroundAAA.png"/>
<Styles>
<LabelStyle ID="Label1" Font="Tahoma" Bold="false" /> <!-- exists in both -->
<LabelStyle ID="Label2" Font="Tahoma" Bold="false" /> <!-- exists in both -->
<LabelStyle ID="Label3" Font="Tahoma" Bold="false" /> <!-- unique -->
</Styles>
</Style>
Input: Xml2
<?xml version="1.0"?>
<Style Width="1024" Height="768">
<BaseStyle Width="1024" Height="768" BackgroundPath="./Images/BackgroundBBB.png"/>
<Styles>
<LabelStyle ID="Label1" Font="Arial" Bold="true" /> <!-- exists in both -->
<LabelStyle ID="Label2" Font="Arial" /> <!-- exists in both -->
<LabelStyle ID="Label4" Font="Arial" Bold="false" /> <!-- unique -->
</Styles>
</Style>
and I need to get the following as output: -
Result: Xml3
<?xml version="1.0"?>
<Style Width="1024" Height="768">
<BaseStyle Width="1024" Height="768" BackgroundPath="./Images/BackgroundBBB.png"/> <!-- has overwritten Xml1 -->
<Styles>
<LabelStyle ID="Label1" Font="Arial" Bold="true" /> <!-- has merged Xml1 & Xml2 with Xml2 taking precedence -->
<LabelStyle ID="Label2" Font="Arial" Bold="false" /> <!-- has merged Xml1 & Xml2 with Xml2 taking precedence (note Bold attribute is present) -->
<LabelStyle ID="Label3" Font="Tahoma" Bold="false" /> <!-- unique -->
<LabelStyle ID="Label4" Font="Arial" Bold="false" /> <!-- unique -->
</Styles>
</Style>
You'll notice from above that anything unique gets merged and anything common (using ID as the key) is updated with Xml2 values.
I've been reading some of the many great questions up here on how to merge 2 xml files using LINQ but none of them really satify my requirement. They all seems to talk about taking A and B and ending up with A+B.
Is there an easy way to do this using LINQ?
Thanks in advance!