Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C语言中合并xml文件#_C#_Xml_Merge - Fatal编程技术网

C# 在C语言中合并xml文件#

C# 在C语言中合并xml文件#,c#,xml,merge,C#,Xml,Merge,我想合并几个xml文件。 目标xml与源文件略有不同。目标文件包含一个传统的根元素 比如说 目标xml: <?xml version="1.0" encoding="utf-8"?> <customer ID="A0001" name="customername"> ..... ..... </customer> 结果(每个订单都是不同的xml文件) 但是我担心使用ReadOuterXml()的大型文件的内存不足 有什么建议吗?在这种情况下,如果所

我想合并几个xml文件。 目标xml与源文件略有不同。目标文件包含一个传统的根元素

比如说

目标xml:

<?xml version="1.0" encoding="utf-8"?>
<customer ID="A0001" name="customername">
.....
.....
</customer>


结果(每个订单都是不同的xml文件)



但是我担心使用ReadOuterXml()的大型文件的内存不足


有什么建议吗?

在这种情况下,如果所有文件都使用UTF-8,那么基本上可以作弊。NET 4使这一点特别容易:

public void MergeFiles(string outputPath, string prefix, string suffix,
                       IEnumerable<string> files)
{
    File.WriteAllText(outputPath, prefix);
    var lines = files.SelectMany(file => File.ReadLines(file).Skip(1));
    File.AppendAllLines(outputPath, lines);
    File.AppendAllText(outputPath, suffix);
}

每个源文件有多大?2G?除了跳过XML声明之外,您还需要对文件进行任何实际处理吗?是否只是添加了前缀和后缀,其余的只是复制?@Cuong Le:是的,有时甚至更大(可以更改-我无法控制)@Jon Skeet:只需要合并所有源文件(跳过xml声明)并添加这些;-)我很困惑,客户标签是如何进入输出的?@Jodrell:你把它放在“前缀”部分。。。我将用一个例子来编辑。啊,我现在明白了。比起我准备的读者-作者链接,我更喜欢简洁。@Jon Skeet:很抱歉我反应太晚了。我一直很忙。很好的解决方案。对我有用。
string myOrder = textReader.ReadOuterXml();
                        writer.WriteRaw(myOrder );
<?xml version="1.0" encoding="utf-8"?>
<customer ID="A0001" name="customername">
    <order number="00001">
        <.....>
        <.....>
        <.....>
    </order>
    <order number="00002">
        <.....>
        <.....>
        <.....>
    </order>
    <order number="00003">
        <.....>
        <.....>
        <.....>
    </order>
</customer>
public void MergeFiles(string outputPath, string prefix, string suffix,
                       IEnumerable<string> files)
{
    File.WriteAllText(outputPath, prefix);
    var lines = files.SelectMany(file => File.ReadLines(file).Skip(1));
    File.AppendAllLines(outputPath, lines);
    File.AppendAllText(outputPath, suffix);
}
string prefix = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n"
              + "<customer ID=\"A0001\" name=\"customername\">";
MergeFiles("output.xml", prefix, "</customer>", sourceFiles);