Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.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# 如何在合并的XML文件中保留格式_C#_Xml_Linq To Xml_Xml Formatting - Fatal编程技术网

C# 如何在合并的XML文件中保留格式

C# 如何在合并的XML文件中保留格式,c#,xml,linq-to-xml,xml-formatting,C#,Xml,Linq To Xml,Xml Formatting,我有两个XML文件,我使用下面的递归代码段将它们合并在一起 var resource1 = XDocument.Load(baseFile, LoadOptions.PreserveWhitespace); var resource2 = XDocument.Load(targetFile, LoadOptions.PreserveWhitespace); // Merge per-element content from secondResource into firstResource

我有两个XML文件,我使用下面的递归代码段将它们合并在一起

var resource1 = XDocument.Load(baseFile, LoadOptions.PreserveWhitespace);
var resource2 = XDocument.Load(targetFile, LoadOptions.PreserveWhitespace);

 // Merge per-element content from secondResource into firstResource            
        foreach (XElement childB in secondResource.Elements())
        {
            // Merge childB with first equivalent childA
            // equivalent childB1, childB2,.. will be combined                
            bool isMatchFound = false;
            foreach (XElement childA in firstResource.Elements())
            {
                if (AreEquivalent(childA, childB))
                {
                    // Recursive merge
                    MergeElements(childA, childB);
                    isMatchFound = true;
                    break;
                }
            }

            // if there is no equivalent childA, add childB into parentA                
            if (!isMatchFound) firstResource.Add(childB);
        }

resource1.Save(outputFile);

我得到了我需要的最终合并文件,但是“>”的每个实例都被替换为
。如何防止这种情况发生?

这是一种自动转换,因为
字符在文本标记语言中是一种特殊的字符。当您通过解析器读回XML时,
>将被转换回。它不应该真的影响。。。谢谢你确认安德烈。我也这么认为。