Programmatically use XSD.exe tool feature (generate schema from class) through .NET Framework classes?

依然范特西╮ 提交于 2019-11-30 20:19:30

Found this which looks like it should do the trick...

public static string GetSchema<T>()
    {
        XmlAttributeOverrides xao = new XmlAttributeOverrides();
        AttachXmlAttributes(xao, typeof(T));

        XmlReflectionImporter importer = new XmlReflectionImporter(xao);
        XmlSchemas schemas = new XmlSchemas();
        XmlSchemaExporter exporter = new XmlSchemaExporter(schemas);
        XmlTypeMapping map = importer.ImportTypeMapping(typeof(T));
        exporter.ExportTypeMapping(map);

        using (MemoryStream ms = new MemoryStream())
        {
            schemas[0].Write(ms);
            ms.Position = 0;
            return new StreamReader(ms).ReadToEnd();
        }
    }
Clovis

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();
        }

}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!