C# 合并 XD​​ocument 中的 XML 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/280313/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 21:23:41  来源:igfitidea点击:

Merge XML files in a XDocument

c#xmllinq-to-xml

提问by Larry

I am trying to merge several XML files in a single XDocument object.

我正在尝试将多个 XML 文件合并到一个 XDocument 对象中。

Merge does not exist in XDocument object. I miss this.

XDocument 对象中不存在 Merge。我想念这个。

Has anyone already implemented a Merge extension method for XDocument, or something similar ?

有没有人已经为 XDocument 或类似的东西实现了 Merge 扩展方法?

采纳答案by Larry

I tried a bit myself :

我自己尝试了一下:

var MyDoc = XDocument.Load("File1.xml");
MyDoc.Root.Add(XDocument.Load("File2.xml").Root.Elements());

I dont know whether it is good or bad, but it works fine to me :-)

我不知道它是好是坏,但它对我来说很好:-)

回答by schnaader

As a workaround, you could use a XSL file to merge the XML files and then transform it to a XDocument object.

作为一种解决方法,您可以使用 XSL 文件来合并 XML 文件,然后将其转换为 XDocument 对象。

回答by Marc Gravell

Being pragmatic, XDocumentvs XmLDocumentisn't all-or-nothing (unless you are on Silverlight) - so if XmlDoucumentdoes something you need, and XDocumentdoesn't, then perhaps use XmlDocument(with ImportNodeetc).

务实,XDocumentvsXmLDocument不是全有或全无(除非你在 Silverlight 上) - 所以如果XmlDoucument做了一些你需要的事情,而XDocument不是,那么也许可以使用XmlDocument(与ImportNode等)。

That said, even with XDocument, you could presumably use XNode.ReadFromto import each, then simply .Addit to the main collection.

也就是说,即使使用XDocument,您大概可以使用XNode.ReadFrom导入每个,然后将其简单地导入.Add主集合。

Of course, if the files are big, XmlReader/XmlWritermight be more efficient... but more complex. Fortunately, XmlWriterhas a WriteNodemethod that accepts an XmlReader, so you can navigate to the first child in the XmlReaderand then just blitz it to the output file. Something like:

当然,如果文件很大,XmlReader/XmlWriter可能效率更高……但更复杂。幸运的是,XmlWriter有一个WriteNode接受 的方法XmlReader,因此您可以导航到 中的第一个子项XmlReader,然后将其快速传送到输出文件。就像是:

    static void AppendChildren(this XmlWriter writer, string path)
    {
        using (XmlReader reader = XmlReader.Create(path))
        {
            reader.MoveToContent();
            int targetDepth = reader.Depth + 1;
            if(reader.Read()) {
                while (reader.Depth == targetDepth)
                {
                    writer.WriteNode(reader, true);
                }                
            }
        }
    }

回答by SV0505

Merge all xml files from dir to one XDocument

将所有 xml 文件从 dir 合并到一个 XDocument

public static XDocument MergeDir(string xmlDir)
{
    XDocument xdoc = XDocument.Parse("<root></root>");
    System.IO.DirectoryInfo directory = new DirectoryInfo(xmlDir);
    if (directory.Exists)
    {
        foreach (System.IO.FileInfo file in directory.GetFiles())
        {
            if (file.Extension == ".xml")
            {
                xdoc.Root.Add(XDocument.Load(file.FullName).Root.Elements());
            }
        }
    }

    return xdoc;
}