[英]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.