If you’re reading this right now, it’s likely because you just discovered that XSD.exe doesn’t honor import declarations within XSD files.
The fix, it turns out, is to manually specify each XSD that the primary XSD requires. Something like xsd.exe primary.xsd imported.xsd another.xsd
In my case, my primary XSD file imported several XSD files… those those imported XSD files imported their own XSD files.. And putting that nasty command line together manually is just too far below my pay grade.
6/7/2016 UPDATE: Per one of the comments below, I have updated the code to support XSD files that reference external schemas via a URI instead of a local file. For each URI encountered, I download the file locally and continue as normal.
("xsd.exe \"" + string.Join("\" \"", files) + "\" /classes").Dump();
private void Do(string file)
file = file.ToLower();
var dir = Path.GetDirectoryName(file);
var contents = File.ReadAllText(file);
var regex = @"schemaLocation=""(.*?)""";
var toProcess = Regex.Matches(contents, regex).OfType<Match>().Select (m => m.Groups.Value).Select (m =>
else if (Uri.IsWellFormedUriString(m, UriKind.Absolute))
var filename = Path.Combine(Path.GetDirectoryName(file), Regex.Replace(m, "[^a-zA-Z0-9]", string.Empty) + ".xsd");
using (var client = new WebClient())
client.DownloadFile(new Uri(m), filename);
return Path.GetFullPath(Path.Combine(dir, m));
}).Select (m => m.ToLower()).Where (m => !files.Contains(m)).ToList();
foreach (var nested in toProcess)
private List<string> files = new List<string>();
It digs through the XSD files recursively AND spitS out exactly what you need to copy/paste into your command line to make XSD do your bidding.
And, by golly, it works well!
Given a sample file from one of the comments below (http://www.mf.gov.pl/documents/764034/5134536/Schemat_JPK_FA%281%29_v1-0.xsd), this utility outputs the following. Note the yellow text at the bottom. That’s the magic line you need to copy/paste into your .NET developer console to generate the scripts.
It’s probably the ugliest code I’ve written in a month. And it was written really quickly. And I can only confirm that it works for my needs. And I tried to do this in Powershell since everyone already has Powershell, but I couldn’t get it to work because Powershell is the devil.
Viva la LINQPad!!!