合并在C#两个XML文件,而无需附加和不删除任何内容(例如给定) [英] Merging two xml files in C# without appending and without deleting anything (example given)

查看:624
本文介绍了合并在C#两个XML文件,而无需附加和不删除任何内容(例如给定)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以说,我有一个XML文件中像这样的:

So say I have one xml file such as this:

<shapes>
    <shape>shape1</shape>
</shapes>

和这样的另一个XML文件:

And another xml file like this:

<parentNode>
    <shapes>
        <shape>shape 2</shape>
    </shapes>
</parentnode>

我想输出是:

I would like the output to be:

<parentNode>
    <shapes>
        <shape>shape1</shape>
        <shape>shape 2</shape>
    </shapes>
</parentnode>

的背景是,我使用的Visio架构,但我希望为其写的Visio XML文件是一个Visio配置文件的一个精简版的应用程序的配置文件。它应该允许用户更改形状属性,例如: 过程有一个黄色的颜色,它应该允许他们增加新的形状,例如AccountsTable该应用程序将使用一个标准的形状之前搜索并使用自定义形状,而不是在某些情况下。

The context is that I am using the visio schema but I wish the config file for an application which writes visio xml files to be a stripped down version of a visio config file. It should allow users to change shape properties, e.g. "process" to have a yellow colour AND it should allow them to add new shapes for example "AccountsTable" which the application will search for before using a standard shape and use the custom shape instead in some circumstances.

在合并方面,它基本上需要坚持正确的叶节点在正确的地方,如果是有道理的?如果没有覆盖任何东西,除非在配置文件已被明确写入这样做,如定制的形状2。

In terms of the merge it basically needs to stick the right leaf nodes in the right places if that makes sense? Without overwriting anything unless the config file has been explicitly written to do so, e.g. a custom "shape 2".

我应该怎么看实现这一目标?该数据集的方法是pretty的没用。

What should I be looking at to achieve this? The dataset method is pretty useless.

非常感谢!

推荐答案

您可以加载这两个文件分为两的XElement 的对象,找到这两个对象的目标节点,添加或删除你的愿望。

You can load both files into two XElement objects, locate the target nodes in both objects and add or remove as you wish.

下面是一个例子:

var doc1 = XDocument.Parse(file1).Element("shapes");
var doc2 = XDocument.Parse(file2).Element("parentNode").Element("shapes");

doc2.Add(doc1.Nodes());

这篇关于合并在C#两个XML文件,而无需附加和不删除任何内容(例如给定)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆