如何合并与C#2 XML文件 [英] How to merge 2 XML files with C#

查看:93
本文介绍了如何合并与C#2 XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个XML文件,我想这两个文件合并为一个。
怎么会呢?我已经尝试了很多,但没有什么帮助。
正如你可以看到合并后的XML已经离开从第二属性的文本,如果它从第一个XML来了。
二元素必须由有ID /姓名/ whatevername第一属性有订购。如果一个节点没有在XML 2存在
第三那么它在同一个地方如在XML中1要创建

I have two XML files and I want to merge this two files to one. But how? I have tried a lot but nothing helps. As you can see the merged XML has left the text from the second attribute if it came from the first XML. Second the Element have to be ordered by there Id/Name/whatevername the first attribute have. Third if a node doesn't exist in XML 2 then it has to be created at the same place as in XML 1.

的XML文件表明这里只是整个XML片段,还有再有更多的属性名称。

The XML files showed here are just a fragment of the whole XML, there 're a lot more attributes names.

我怎么能做到这一点的C#?

How can I do this with C#?

XML 1

<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>
    <POP Id="Green" Value="Monster"/>
    <POP Id="Blue" Value="Doggie"/>



XML 2

<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>



XML应该是这样的后合并:

XML should look like this after merge:

<APPLICATION>
    <AC>
            <CLASS Name="Hello1" Capt="dodo1"/>
            <CLASS Name="Hello2" Capt="dodo2"/>
            <CLASS Name="Hello3" Capt="dodo3"/>
            <CLASS Name="Hello5" Capt=""/>
            <CLASS Name="Hello8" Capt=""/>
            <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>
            <ITEM Id="11" DefaultValue=""/>
            <ITEM Id="12" DefaultValue="">
                    <VAL>
                            <REASON Id="Job1" SecondOne=""/>
                    </VAL>
            </ITEM>
    </BO>
    <POP Id="Green" Value=""/>
    <POP Id="Blue" Value=""/>



感谢名单所有的答案,但我仍然有我不知道的标记名是如何,所以我上无法硬编码标签的问题。

Thanx for all the answers, but still I have the problem that I don't know how the tagnames are, so I cann't hardcode the tags.

我只是给你一个例子什么它可以像。但下一次,我让我的XML文件,从上面的标签可以是完全不同的。这就是问题的所在。所以我说,水湿新的XElement(BO,boChildren),因为下一次这个标签已经不存在了。

I just have to give you an example what it can look like. But the next time I get my XML files, the tags from above can be totally different. That is where the problem is. So I cann't say new XElement("BO", boChildren), because the next time this tag doesn't exist anymore.

或者我水湿这个硬编码==> VAR汽车= xDocuments.SelectMany(X => x.Root.Elements(CARS))合并();因为下一次,我让我的XML文件的汽车总动员已经不存在了。

Or I cann't hardcode this ==> var cars = xDocuments.SelectMany(x => x.Root.Elements("CARS")).Merge(); because the next time I get my XML files "CARS" doesn't exist anymore.

推荐答案

我认为你可以做到这一点LINQ到XML。请为您共同加入他们的行列,然后把这些组合成一个新的文档每个段(AC,BO,汽车,POP)单独的查询。

I think you can do this with Linq to XML. Create separate queries for each segment (AC, BO, CARS, POP) where you join them together and then put those together into a new document.

下面是一个小片段,让你开始:

Here's a little snippet to get you started:

using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

namespace XML_Merge {
    class Program {
        static void Main(string[] args) {

            // load two xdocs
            var x1 = XDocument.Load("x1.xml");
            var x2 = XDocument.Load("x2.xml");

            // select the CLASS nodes from each
            var c1 = x1.Descendants("AC").First().Descendants("CLASS");
            var c2 = x2.Descendants("AC").First().Descendants("CLASS");

            // this one gives the distinct union of the two, you can put that into the result xdoc.
            var cComb =
                c1
                .Union(c2)
                .Distinct(new ClassComparer()) // this uses the IEqualityComparer from below
                .OrderBy(c => c.Attribute("Name").Value);
        }
    }

    // This is required for Union to work. (Also Intersect etc)
    class ClassComparer : IEqualityComparer<XElement> {
        public bool Equals(XElement x, XElement y) { return x.Attribute("Name").Value == y.Attribute("Name").Value; }
        public int GetHashCode(XElement obj) { return obj.Attribute("Name").Value.GetHashCode(); }
    }
}



只是重复源文档中的其他节点然后把它放在一起。

Just repeat for the other nodes in the source docs and then put it all together.

祝你好运,

格特 - 扬

这篇关于如何合并与C#2 XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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