Prash's Blog

Bulk Xml Schema Generation and Xml Validation January 17, 2014

Filed under: C# — prazjain @ 3:22 pm
Tags: , ,

I got into a situation where I had 250+ xml files with business information (logic + data), and the code was reliant on it being correctly typed by another external business facing team.
I wrote this quick xml schema generator and validator for helping me in that process.

This is what I did:

1) Generate a xml schema for each of those xml files using this code below

public class MultibookXsdGenerator
{
public void Generate()
{
string xmlFilesDir = @”C:\Source\Grids”;
string outputDir = @”C:\XsdGenerator”;
string copyToDirectory = “xmls”;
string xsdDirectory = “xsds”;

if (!Directory.Exists(Path.Combine(outputDir, copyToDirectory)))
{
Directory.CreateDirectory(Path.Combine(outputDir, copyToDirectory));
}
if (!Directory.Exists(Path.Combine(outputDir, xsdDirectory)))
{
Directory.CreateDirectory(Path.Combine(outputDir, xsdDirectory));
}

string[] xamlFiles = Directory.GetFiles(xamlFilesDir, “*.xaml”);
foreach (var xamlFile in xamlFiles)
{
FileInfo fileInfo = new FileInfo(xamlFile);
FileInfo xmlFileInfo = new FileInfo(Path.Combine(outputDir, copyToDirectory, fileInfo.Name.Replace(“.xaml”, “.xml”)));
File.Copy(xamlFile, xmlFileInfo.FullName);
ProcessStartInfo oInfo = new ProcessStartInfo(Environment.ExpandEnvironmentVariables(“\”C:\\Program Files (x86)\\Microsoft SDKs\\Windows\\v7.0A\\Bin\\x64\\xsd.exe\””), “/o:” + Path.Combine(outputDir, xsdDirectory) + ” ” + xmlFileInfo.FullName);
oInfo.UseShellExecute = false;
oInfo.ErrorDialog = false;
oInfo.CreateNoWindow = true;
oInfo.RedirectStandardOutput = true;
try
{
Process p = Process.Start(oInfo);
StreamReader oReader2 = p.StandardOutput;
string sRes = oReader2.ReadToEnd();
oReader2.Close();
// sRes now contains the output from xsd.exe
//File.Move(xmlFileInfo.FullName.Replace(“.xml”,”.xsd”),Path.Combine(outputDir,xsdDirectory));
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}

2) Start to manually merge those xsd files to one single xsd. This is basically go to each

3) Validate the all the xml files with the main schema using the code below.

public class Validator
{
// Validation Error Count
static int ErrorsCount = 0;
// Validation Error Message
static string ErrorMessage = “”;
public static void ValidationHandler(object sender,
ValidationEventArgs args)
{
ErrorMessage = ErrorMessage + args.Message + “\r\n”;
ErrorsCount++;
}
public void Validate()
{
string dirName = @”C:\XsdGenerator\xmls”;
int fileCount = 0;
foreach (var file in Directory.GetFiles(dirName,”*.xml”))
{
fileCount++;
Console.WriteLine(“File number : ” + fileCount);
try
{
string strXMLDoc = File.ReadAllText(file);
// Declare local objects
XmlDocument xmld = new XmlDocument();
xmld.LoadXml(strXMLDoc);
xmld.Schemas.Add(null, @”./Xsds/Main.xsd”);
xmld.Validate(ValidationEventHandler);
// XML Validation succeeded
Console.WriteLine(“XML validation succeeded.\r\n”);
}
catch (Exception error)
{
// XML Validation failed
Console.WriteLine(“XML validation failed.” + “\r\n” +
“Error Message: ” + error.Message);
}

}

Console.WriteLine(“Console Messages : ” + Validator.ErrorMessage);
Console.WriteLine(“Console Error count : ” + Validator.ErrorsCount);
Console.ReadLine();
}
}

Advertisements