Skip to content

Instantly share code, notes, and snippets.

@stdray
Created December 12, 2014 17:44
Show Gist options
  • Select an option

  • Save stdray/c41a83f50cc6e67cf00e to your computer and use it in GitHub Desktop.

Select an option

Save stdray/c41a83f50cc6e67cf00e to your computer and use it in GitHub Desktop.
open System.IO;
open System.Xml.Serialization;
open System.Xml.Schema;
open System.CodeDom;
open System.CodeDom.Compiler;
open Microsoft.CSharp
type Options = {
rootPath : string;
xsdPath : string;
namespaceName : string;
outputPath : string;
}
let xsd2Cs options =
let readXsd path = use s = File.OpenRead(path) in XmlSchema.Read(s, null)
let xsdPath = Path.Combine(options.rootPath, options.xsdPath)
let xsds = XmlSchemas()
let xsd = xsdPath |> readXsd;
xsd |> (xsds.Add >> ignore)
Directory.GetFiles(options.rootPath, "*.xsd", SearchOption.AllDirectories)
|> Seq.filter(fun f -> f <> xsdPath)
|> Seq.map readXsd
|> Seq.iter(xsds.Add >> ignore)
xsds.Compile(null, true)
let importer = XmlSchemaImporter(xsds)
let codeNamespace = CodeNamespace(options.namespaceName)
let exporter = XmlCodeExporter(codeNamespace)
xsd.SchemaTypes.Values
|> Seq.cast<XmlSchemaType>
|> Seq.map (fun t -> importer.ImportSchemaType(t.QualifiedName))
|> Seq.iter exporter.ExportTypeMapping
xsd.Elements.Values
|> Seq.cast<XmlSchemaElement>
|> Seq.map (fun t -> importer.ImportTypeMapping(t.QualifiedName))
|> Seq.iter exporter.ExportTypeMapping
CodeGenerator.ValidateIdentifiers(codeNamespace)
let codeProvider = new CSharpCodeProvider()
use writer = new StreamWriter(options.outputPath, false)
codeProvider.GenerateCodeFromNamespace(codeNamespace, writer, CodeGeneratorOptions())
xsd2Cs {
rootPath = @"C:\prj\Marketing Portal\ETL\Adapters integration\Schemes";
xsdPath = @"Plans\Plan.xsd";
namespaceName = "Yoba";
outputPath = @"D:\Test.cs"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment