问题
Im setting up an XML Translator and before this step, I have to compare two XML Files, filter out the changes or entrys that are new and save only the changes in a new file.
With XmlDiffPatch
I was able to compare the two files and save it in a DiffGram format. But when I patch the diffGramFile
and the originalFile
, the output is just like my originalFile
so I win nothing out of it. Is there a way to delete duplicates of two files or like only save the changes?
This is my code to generate a diffGram and Patch it up.
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();
}
Input files: File 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>
File 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>
and finaloutput
File equals File 1..
hope for some help.
回答1:
Try 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);
}
}
}
来源:https://stackoverflow.com/questions/57885510/how-to-get-only-changes-of-two-xml-files-in-c-sharp-context