繁体   English   中英

获取C#类型的XSD架构

[英]Get XSD schema for C# type

如何为ac#type生成xsd模式(在代码中)。 肯定有一种方法,因为为wcf中的数据交换生成了xsd模式。

比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

所以,我通过在xsd.exe中查看反射器找到了我的问题的解决方案。 这是供将来参考:

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

暂无
暂无

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

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