Get XSD Schema for C # Type

how can i generate xsd schema for type c # (in code). There must be a path, of course, because the xsd schema is generated for datacontracts in wcf.

+4
source share
3 answers

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

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

Going a bit further than s7orm's answer, I wrote this simple function that I got from reflection 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); } } 
+2
source

You can use the XML Schema Definition Tool (xsd.exe)

 xsd.exe YourAssembly.dll /type:YourNamespace.YourType 
+1
source

Source: https://habr.com/ru/post/1308597/


All Articles