Accendo / Publikované

XSD C# Comparator

Autor:  Libor Bešenyi

Dátum:  19.2.2014

 

There is necessary sometimes to compare 2 XSD files – to check if has been interface changed. We use it in unit test in combination with Microsoft XSD tool. I’m not sure why, but sometimes is XSD for the same structure generated in different order. It can be caused for example with compilation symbol (DEBUG <> RELEASE). That means we were not able to compare plain texts (because we got lot of differences).

 

However XSD is defined in XML format so it is not such complicated to compare 2 xmls, so we have written tool.

 

Please note: It was not tested pretty much, but source code is included in the project and you can fix / update source code as you wish.

 

Example of usage:

 

[TestMethod]

public void CheckExternalLodgementRequest()

{

        Helper.CheckSchemes<Elmhurst.Eco2012Web.Services.Interface.ExternalUploadRequest>();

}

 

public static void CheckSchemes<T>()

{

        string lastSchemaFolder = Path.Combine(Init.SvnRoot, "Schema");

 

        // Creates XSD from class (web dll)

        string fileName = Path.Combine(lastSchemaFolder, "xsd.exe");

 

        string webDllPath = Path.Combine((typeof(T).Assembly.ManifestModule).Name);

        string args = webDllPath + " /type:" + typeof(T).Name;

 

        var output = Elmhurst.Utils.Windows.Execute(fileName, args);

        Assert.IsNotNull(output);

        Assert.IsTrue(output.Contains("Writing file"));

 

        // Update XSD

        fileName = Path.Combine(lastSchemaFolder, "NillToMinOccurs.exe");

 

        string currentSchemaFile = Path.Combine("schema0.xsd");

        args = "\"" + currentSchemaFile + "\"";

 

        output = Windows.Execute(fileName, args);

        Assert.IsNotNull(output);

        Assert.IsFalse(output.Contains("doesn't exist!"), "Schema file has not been updated");

 

        // Compare current schema with latest used schema version

        string lastUsedSchemaFile = Path.Combine(Init.SvnRoot, "Schema", typeof(T).Name + ".xsd");

        string lastUsedSchemaContent = File.ReadAllText(lastUsedSchemaFile);

 

        string currentSchemaContent = File.ReadAllText(currentSchemaFile);

 

        // Check if is old interface compatible with new -if not CLIENTS SHOULD BE AMENDED !!!

        var comparator = new Elmhurst.Utils.XsdComparator(lastUsedSchemaContent, currentSchemaContent);

        comparator.Compare();

 

        Assert.IsFalse(comparator.IsError, string.Join(" | ", comparator.Differences));

 

        // Check if was added something new (new schema must be regenerated)

        comparator = new Elmhurst.Utils.XsdComparator(currentSchemaContent, lastUsedSchemaContent);

        comparator.Compare();

 

        Assert.IsFalse(comparator.IsError, string.Join(" | ", comparator.Differences));

}

 

Download Example Project