如何仅获取 c# 上下文中两个 xml 文件的更改?
Posted
技术标签:
【中文标题】如何仅获取 c# 上下文中两个 xml 文件的更改?【英文标题】:How to get changes only, of two xml files in c# context? 【发布时间】: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)。 【参考方案1】:试试 xml linq:
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication131
class Program
const string INPUT_XML1 = @"c:\temp\test.xml";
const string INPUT_XML2 = @"c:\temp\test1.xml";
const string OUTPUT_XML = @"c:\temp\test2.xml";
static void Main(string[] args)
XDocument doc1 = XDocument.Load(INPUT_XML1);
XElement body1 = doc1.Descendants("body").FirstOrDefault();
XDocument doc2 = XDocument.Load(INPUT_XML2);
XElement body2 = doc2.Descendants("body").FirstOrDefault();
var query1 = (from d1 in body1.Elements()
join d2 in body2.Elements() on d1.ToString() equals d2.ToString() into p
from d2 in p.DefaultIfEmpty()
select new d1 = d1, d2 = p == null ? null : d2 )
.Where(x => x.d2 == null)
.Select(x => x.d1)
.ToList();
var query2 = (from d2 in body2.Elements()
join d1 in body1.Elements() on d2.ToString() equals d1.ToString() into p
from d1 in p.DefaultIfEmpty()
select new d2 = d2, d1 = p == null ? null : d1 )
.Where(x => x.d1 == null)
.Select(x => x.d2)
.ToList();
XElement newBody = new XElement("body", query1);
newBody.Add(query2);
body1.ReplaceWith(newBody);
doc1.Save(OUTPUT_XML);
【讨论】:
对我来说效果很好,但第二个查询 (query2) 仍然没有必要,因为它会返回未更改的值,因此输出中的更改条目有两个条目。 当我测试时,我从每个列表中删除了不同的项目。两个文件必须有细微差别才能放回相同的项目。该代码正在测试整个元素,因此有些不同(可能是间距或小写/大写差异)。以上是关于如何仅获取 c# 上下文中两个 xml 文件的更改?的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 C# 获取给定路径的完整路径(可以是目录或文件,甚至是完整路径)?