I want to generate an XML Schema based upon a class, just as you can do with the Xsd.exe tool.
E.g. xsd.exe /type: typename /outputdir:c:\\ assmeblyname
do this:
public string GetFullSchema() {
string @namespace = "yourNamespace";
var q = from t in Assembly.GetExecutingAssembly().GetTypes()
where t.IsClass && t.Namespace == @namespace
select t;
XmlReflectionImporter importer = new XmlReflectionImporter(@namespace);
XmlSchemas schemas = new XmlSchemas();
XmlSchemaExporter exporter = new XmlSchemaExporter(schemas);
foreach (var x in q)
{
var map = importer.ImportTypeMapping(x);
exporter.ExportTypeMapping(map);
}
using (MemoryStream ms = new MemoryStream())
{
schemas[0].Write(ms);
ms.Position = 0;
return new StreamReader(ms).ReadToEnd();
}
}