【发布时间】:2015-06-25 18:51:43
【问题描述】:
我有多个这种格式的 xml 流:
xml1:
<?xml version="1.0" encoding="utf-8"?>
<Node>
<NodeA>test1</NodeA>
<NodeB>test2</NodeB>
<NodeC>
<Att>test4</Att>
<Value1>1.0</Value1>
</NodeC>
</Node>
<Node>
<NodeA>test1</NodeA>
<NodeB>test7</NodeB>
<NodeC>
<Att>test8</Att>
<Value1>2.0</Value1>
</NodeC>
</Node>
...
xmlN:
<?xml version="1.0" encoding="utf-8"?>
<Node>
<NodeA>test1</NodeA>
<NodeB>test2</NodeB>
<NodeC>
<Att>test4</Att>
<ValueN>5.0</ValueN>
</NodeC>
</Node>
<Node>
<NodeA>test1</NodeA>
<NodeB>test7</NodeB>
<NodeC>
<Att>test8</Att>
<ValueN>6.0</ValueN>
</NodeC>
</Node>
<Node>
<NodeA>test9</NodeA>
<NodeB>test8</NodeB>
<NodeC>
<Att>test8</Att>
<ValueN>6.0</ValueN>
</NodeC>
</Node>
我希望合并后的 xml 看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<Node>
<NodeA>test1</NodeA>
<NodeB>test2</NodeB>
<NodeNEW>test4</Att>
<Value1>1.0</Value1>
<ValueN>5.0</ValueN>
</Node>
<Node>
<NodeA>test1</NodeA>
<NodeB>test7</NodeB>
<NodeNEW>test8</NodeNEW>
<Value1>2.0</Value1>
<ValueN>6.0</ValueN>
</Node>
<Node>
<NodeA>test9</NodeA>
<NodeB>test8</NodeB>
<NodeNEW>test8</NodeNew>
<Value1></Value1>
<ValueN>6.0</ValueN>
</Node>
所以我创建了具有不同 Value1 的唯一键 NodeA>NodeB>NodeNEW,如果此唯一键未出现在其各自的 xml 中,则不会分配任何内容。
最有效的方法是什么?
【问题讨论】:
-
反序列化、合并、序列化。
-
您是否尝试过创建相应的模型,然后使用
xmlElement等xml 属性标记属性,然后加载您的模型。然后以编程方式将它们合并到第三个模型中,然后使用 xml 序列化器序列化到新的 xml 中。请参阅此处的代码msdn.microsoft.com/en-us/library/…