根据条件合并 XML
本文关键字:XML 合并 条件 | 更新日期: 2023-09-27 18:37:03
我有两个xml就是这样说的。如下
.XML:
<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>
XMl2:
<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>
我需要如下输出:
条件:我想要一个关于 ID 的条件,这样 - 当不同 XMl 上的两个"ID"相等时,无论其他属性如何。相同的 ID 值应打印为 One,无论其属性如何(无论是否不同)。我需要如下输出
合并Xml:
<APPLICATION>
<AC>
<CLASS Name="Hello1" Capt="do1"/>
<CLASS Name="Hello2" Capt="do2"/>
<CLASS Name="Hello5" Capt="do5"/>
<CLASS Name="Hello8" Capt="do8"/>
<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="name1"/>
<ITEM Id="3" DefaultValue="name3"/>
<ITEM Id="11" DefaultValue="name11"/>
<ITEM Id="12" DefaultValue="name12">
<ITEM Id="9" DefaultValue="name11"/>
<ITEM Id="10" DefaultValue="name12">
</BO>
我需要用 C# 编写
好的,这应该让你进入球场。
Dim x1 As XElement
Dim x2 As XElement
x1 = <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>
</APPLICATION>
x2 = <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>
</APPLICATION>
Dim ie As IEnumerable(Of XElement)
ie = From x1id In x1.<BO>.<ITEM>, x2id In x2.<BO>.<ITEM>
Where x1id.@Id = x2id.@Id
Select x2id
If ie.Count > 0 Then
ie.Remove()
x1.<AC>.LastOrDefault.Add(x2.<AC>.Elements)
x1.<AC>.LastOrDefault.AddAfterSelf(x2.<CARS>)
x1.<BO>.LastOrDefault.Add(x2.<BO>.Elements)
End If