1

我正在尝试合并两个 XML 文档。第一个是具有默认值的模板,第二个具有相同的结构,但缺少字段。我想加载这两个文件并使用第一个文件中给出的默认值填充第二个 XML 文件的缺失字段。例如 :

默认配置.xml:

<CollectionItem>
   <Item>
      <var1>10</var1>
      <var2>20</var2>
   </Item>
</CollectionItem>

配置1.xml:

<CollectionItem>
   <Item>
      <var1>5</var1>
   </Item>
   <Item>
      <var2>5</var2>
   </Item>
</CollectionItem>

因此,我希望输出文件看起来像:

<CollectionItem>
   <Item>
      <var1>5</var1>
      <var2>20</var2>
   </Item>
   <Item>
      <var1>10</var>
      <var2>5</var2>
   </Item>
</CollectionItem>

此外,我想是通用的,如果我在节点项目中添加一个字段,我不想对其进行编码,而是在默认的 XML 文件中读取它。谢谢你的帮助 !

4

2 回答 2

0

使用XElemnt类读取 XML 文件。您可以使用 Elements() 方法获取子元素。每个元素都有一个属性“名称”。如果非默认元素没有默认元素具有的字段 - 使用 Add 函数添加它。

最后不要忘记调用函数“保存”。

于 2012-08-01T17:56:55.300 回答
0

好的,简单的方法,我只是想知道是否还没有一种方法可以做到这一点。我知道两个数据集的合并功能可以将结构与属性“转换”为元素或元素合并为遵循 gicen 方案的属性....

更进一步,如果这两个文件只是一个这样的文件怎么办:

<CollectionItem>
   <Item>
      <Name>default</Name>
      <var1>5</var1>
      <var2>20</var2>
   </Item>
   <Item>
      <Name>config1</Name>
      <var1>10</var>
   </Item>
</CollectionItem>

and while deserialization into a class "CollectionItem", I'd like to populate the second Item with its values plus the default values that I find in the first Item if not given ? It is quite simple to do it after deserialization, but is it possible possible to do it AT deserialization. With binary files it is also easy with the interface IOnDeseralizationCallback, but this concept does not exist with XML files...

于 2012-08-02T11:26:48.183 回答