我需要将2个xml文件“合并”在一起,但要考虑在两者中出现的元素和合并属性,这些属性在每个属性中都不相同。下面是我的意思的例子:使用LINQ合并2个Xml文件
输入: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>
输入: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>
,我需要得到以下的输出: -
结果: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>
您会注意到,从上面可以看出,任何唯一的东西都会被合并,任何常见的东西(使用ID作为关键字)都会使用Xml2值进行更新。
我一直在阅读一些关于如何使用LINQ合并2个xml文件的很多好问题,但他们都没有真正满足我的要求。他们似乎都在谈论拿A和B并以A + B结束。
有没有一种简单的方法来使用LINQ来做到这一点?
在此先感谢!
你见过这样的:** HTTP://stackoverflow.com/questions/4937875/merging -two-xml-files-linq ** –