如何仅获取c#上下文中两个xml文件的更改? [英] How to get changes only, of two xml files in c# context?

查看:87
本文介绍了如何仅获取c#上下文中两个xml文件的更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要设置一个XML转换器,在此步骤之前,我必须比较两个XML文件,过滤掉新的更改或条目,并仅将更改保存在一个新文件中。

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.

使用 XmlDiffPatch ,我可以比较两个文件并将其保存为DiffGram格式。但是,当我修补 diffGramFile originalFile 时,输出就像我的 originalFile ,所以我什么也赢不了。有没有办法删除两个文件的重复项或只保存更改?

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?

这是我的代码,用于生成diffGram并将其修补。

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();

        }

输入文件:
文件1:测试。 xml

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>

文件2:test2.xml

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>

finaloutput 文件等于文件1。

希望获得帮助。

推荐答案

尝试xml linq:

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);

        }
    }




}

这篇关于如何仅获取c#上下文中两个xml文件的更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆