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