简体   繁体   中英

Get XSD schema for C# type

how can I generate a xsd schema for ac# type (in code). There must be certainly a way, because xsd schema is generated for datacontracts in wcf.

Going a bit farther than s7orm's answer I wrote this simple function that I derived from reflecting on xsd.exe:

private void ExtractXsdFromType(Type type, FileInfo xsd)
    {
        XmlReflectionImporter importer = new XmlReflectionImporter();
        XmlTypeMapping mapping = importer.ImportTypeMapping(type);
        XmlSchemas xmlSchemas = new XmlSchemas();
        XmlSchemaExporter xmlSchemaExporter = new XmlSchemaExporter(xmlSchemas);            

        using (FileStream fs = xsd.Create())
        {
            xmlSchemaExporter.ExportTypeMapping(mapping);
            xmlSchemas[0].Write(fs);
        }           
    }

您可以使用XML架构定义工具(xsd.exe)

xsd.exe YourAssembly.dll /type:YourNamespace.YourType

So, I found a solution to my problem by looking with reflector in the xsd.exe. Here is it for future reference:

XmlReflectionImporter importer = new XmlReflectionImporter();
XmlTypeMapping stringMapping = importer.ImportTypeMapping(typeof(String));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM