【发布时间】:2020-01-13 01:55:09
【问题描述】:
我正在设置一个 XML 转换器,在此步骤之前,我必须比较两个 XML 文件,过滤掉新的更改或条目,并仅将更改保存在新文件中。
使用XmlDiffPatch,我能够比较这两个文件并将其保存为 DiffGram 格式。但是当我修补diffGramFile 和originalFile 时,输出就像我的originalFile 一样,所以我一无所获。有没有办法删除两个文件的重复项或只保存更改?
这是我生成 diffGram 并对其进行修补的代码。
static void Main(string[] args)
{
string file1 = "C:\\temp\\test.xml";
string file2 = "C:\\temp\\test2.xml";
string output = "C:\\temp\\output.xml";
string finaloutput = "C:\\temp\\final.xml";
//DiffXmlStrict(file1, file2);
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
XmlWriter writer = XmlWriter.Create(output, settings);
GenerateDiffGram(file1, file2, writer);
PatchUp(file2, output, finaloutput);
}
public static void GenerateDiffGram(string finalFile, string originalFile, XmlWriter diffGramWriter)
{
XmlDiff xmldiff = new XmlDiff(XmlDiffOptions.IgnoreChildOrder |
XmlDiffOptions.IgnoreNamespaces |
XmlDiffOptions.IgnorePrefixes);
bool bIdentical = xmldiff.Compare(originalFile, finalFile, false, diffGramWriter);
diffGramWriter.Close();
}
public static void PatchUp(String originalFile, String diffGramFile, String OutputFile)
{
XmlDocument sourceDoc = new XmlDocument(new NameTable());
sourceDoc.Load(originalFile);
XmlTextReader diffgramReader = new XmlTextReader(diffGramFile);
XmlPatch xmlPatch = new XmlPatch();
xmlPatch.Patch(sourceDoc, diffgramReader);
XmlTextWriter output = new XmlTextWriter(OutputFile, Encoding.Unicode);
sourceDoc.Save(output);
output.Close();
}
输入文件: 文件1:test.xml
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<xliff version="1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://docs.oasis-open.org/xliff/v1.2/os/xliff-core-1.2-transitional.xsd">
<file source-language="en" datatype="plaintext" date="2016-02-08T14:15:00Z">
<header/>
<body>
<trans-unit datatype="plaintext" id="ErrorCode.1001" resname="ErrorCode.1001" >
<source>Call not implemented.</source>
</trans-unit>
<trans-unit datatype="plaintext" id="ErrorCode.1002" resname="ErrorCode.1002" >
<source>Cannot copyy %s.</source>
</trans-unit>
<trans-unit datatype="plaintext" id="ErrorCode.1003" resname="ErrorCode.1003" >
<source>Cannot create all parameters for %s.</source>
</trans-unit>
<trans-unit datatype="plaintext" id="ErrorCode.1004" resname="ErrorCode.1004" >
<source>Cannot create %e for %s.</source>
</trans-unit>
<trans-unit datatype="plaintext" id="ErrorCode.1005" resname="ErrorCode.1005" >
<source>Cannot delete all parameters for %s.</source>
</trans-unit>
<trans-unit datatype="plaintext" id="ErrorCode.1006" resname="ErrorCode.1006" >
<source>Cannot find %s.</source>
</trans-unit>
<trans-unit datatype="plaintext" id="ErrorCode.1007" resname="ErrorCode.1007" >
<source>Cannot get %s name.</source>
</trans-unit>
<trans-unit datatype="plaintext" id="ErrorCode.1008" resname="ErrorCode.1008" >
<source>Cannot get object.</source>
</trans-unit>
<trans-unit datatype="plaintext" id="ErrorCode.1009" resname="ErrorCode.1009" >
<source>Cannot get parameter.</source>
</trans-unit>
<trans-unit datatype="plaintext" id="ErrorCode.1010" resname="ErrorCode.1010" >
<source>Cannot load document for %s.</source>
</trans-unit>
<trans-unit datatype="plaintext" id="ErrorCode.1011" resname="ErrorCode.1011" >
<source>Cannot unload document for %s.</source>
</trans-unit>
<trans-unit datatype="plaintext" id="ErrorCode.1012" resname="ErrorCode.1012" >
<source>Cannot reload document for %s.</source>
</trans-unit>
</body>
</file>
</xliff>
文件 2:test2.xml
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<xliff version="1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://docs.oasis-open.org/xliff/v1.2/os/xliff-core-1.2-transitional.xsd">
<file source-language="en" datatype="plaintext" date="2016-02-08T14:15:00Z">
<header/>
<body>
<trans-unit datatype="plaintext" id="ErrorCode.1001" resname="ErrorCode.1001" >
<source>Call not implemented.</source>
</trans-unit>
<trans-unit datatype="plaintext" id="ErrorCode.1002" resname="ErrorCode.1002" >
<source>Cannot copy %s.</source>
</trans-unit>
<trans-unit datatype="plaintext" id="ErrorCode.1003" resname="ErrorCode.1003" >
<source>Cannot create all parameters for %s.</source>
</trans-unit>
<trans-unit datatype="plaintext" id="ErrorCode.1004" resname="ErrorCode.1004" >
<source>Cannot create %e for %s.</source>
</trans-unit>
<trans-unit datatype="plaintext" id="ErrorCode.1005" resname="ErrorCode.1005" >
<source>Cannot delete all parameters for %s.</source>
</trans-unit>
<trans-unit datatype="plaintext" id="ErrorCode.1006" resname="ErrorCode.1006" >
<source>Cannot find %s.</source>
</trans-unit>
<trans-unit datatype="plaintext" id="ErrorCode.1007" resname="ErrorCode.1007" >
<source>Cannot get %s name.</source>
</trans-unit>
<trans-unit datatype="plaintext" id="ErrorCode.1008" resname="ErrorCode.1008" >
<source>Cannot get object.</source>
</trans-unit>
<trans-unit datatype="plaintext" id="ErrorCode.1009" resname="ErrorCode.1009" >
<source>Cannot get parameter.</source>
</trans-unit>
</body>
</file>
</xliff>
并且finaloutput 文件等于文件 1..
希望得到一些帮助。
【问题讨论】:
-
如果我理解正确的话,你想要的只是一个增量。在这种情况下,您不应该修补原始文件(无论如何它是过度完成的文件,因此它已经包含增量),而是使用 diffGramFile 代替。它已经包含增量,对吧?如果需要,只需应用 xslt 变换将其变为所需的形状(xliff 1.2)。