简体   繁体   English

获取C#类型的XSD架构

[英]Get XSD schema for C# type

how can I generate a xsd schema for ac# type (in code). 如何为ac#type生成xsd模式(在代码中)。 There must be certainly a way, because xsd schema is generated for datacontracts in wcf. 肯定有一种方法,因为为wcf中的数据交换生成了xsd模式。

Going a bit farther than s7orm's answer I wrote this simple function that I derived from reflecting on xsd.exe: 比s7orm的答案更远,我写了这个简单的函数,我从反映在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. 所以,我通过在xsd.exe中查看反射器找到了我的问题的解决方案。 Here is it for future reference: 这是供将来参考:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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