Knowledge base

IT is a great and exciting world to be in.
All who have passion for it will understand why we have created these pages.
Here we share our technical knowledge with you.
ProgrammingAICloudManagementMobileDB & AnalysisSafetyOtherArchitectureTips

XSD Comparation in C#

19/2/2014

Libor Bešenyi (Solution architect)

It is  sometimes necessary to compare 2 XSD files – to check if the interface has been amended. We use it for instance in unit tests in combination with Microsoft XSD tool. I don't know why, but sometimes XSD for the same structure generated elements in the output file is in a different order. That means we are not able to compare two interface outputs as plain texts.

However, XSD is basically just XML, so it is not so tricky to compare 2 xmls. We wrote  a tool for you. Example of use:

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

         // *** EXAMPLE ***
         // 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));
}

See more: Web services & Xsd basic for .net

Find out more

ProgrammingArchitectureTips

Quick start

1
Contact
Contact us for a free consultation on your business needs!
2
Analysis
After the discussion we will perform mapping of the processes and analyse the current state.
3
Proposal
You will receive variety of scenarios to choose from discribing different ways how to solve your issue.
Contact us