Is there T4 templates available for generating c# classes from xsd?

一个人想着一个人 提交于 2019-12-21 17:56:45

问题


Is there T4 templates available for generating c# classes from xsd?


回答1:


Not that I know of, but take a look at LINQ to XSD (http://linqtoxsd.codeplex.com/). You can use LinqToXsd.exe to generate strongly typed classes based on your schema. And then you have full LINQ support as well. Very handy.

You could also set up a pre-build event on your project that looks something like:

"$(ProjectDir)Lib/LinqToXsd/LinqToXsd.Exe" "$(ProjectDir)MySchema.xsd" /fileName:MySchema.cs

And that would generate the classes from the schema right before you build, so if you change your schema, your classes will stay in sync with each build.




回答2:


I just built a very simple one today that should do the trick.

<#@ template debug="true" hostSpecific="true" #>
<#@ Assembly Name="System.Core.dll" #>
<#@ Assembly Name="System.Windows.Forms.dll" #>
<#@ Assembly Name="System.Xml" #>
<#@ Assembly Name="Microsoft.CSharp" #>
<#@ output extension=".txt" #>
<#@ import namespace="System" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Reflection" #>
<#@ import namespace="System.Xml" #>
<#@ import namespace="System.Xml.Serialization" #>
<#@ import namespace="System.Xml.Schema" #>
<#@ import namespace="System.CodeDom" #>
<#@ import namespace="System.CodeDom.Compiler" #>
<#@ import namespace="Microsoft.CSharp" #>
<# 
    // directory of this template
    var outputDirectory = Path.GetDirectoryName(Host.TemplateFile);

    // iterate through each XSD file in our /Schema/ directory
    // and output the generated C# file in this directory.
    foreach(var file in new DirectoryInfo(Host.ResolvePath("Schemas")).GetFiles("*.xsd")) {

        // ouput file should be the directory of this template, with .Generated.cs
        var outputFile = Path.Combine(outputDirectory, file.Name.Replace(".xsd", ".Generated.cs"));

        // do it
        File.WriteAllText(outputFile, GenerateFromXsd(file.FullName));
    }
#>
<#+
    private string GenerateFromXsd(string xsdFileName)
    {
        // load the xsd
        XmlSchema xsd;
        using (FileStream stream = new FileStream(xsdFileName, FileMode.Open, FileAccess.Read))
        {
            xsd = XmlSchema.Read(stream, null);
        }

        var xsds = new XmlSchemas();
        xsds.Add(xsd);
        xsds.Compile(null, true);

        var schemaImporter = new XmlSchemaImporter(xsds);

        // create the codedom
        var codeNamespace = new CodeNamespace((string)System.Runtime.Remoting.Messaging.CallContext.LogicalGetData("NamespaceHint"));
        var codeExporter = new XmlCodeExporter(codeNamespace);

        var maps = new List<object>();
        foreach (XmlSchemaType schemaType in xsd.SchemaTypes.Values)
        {
            maps.Add(schemaImporter.ImportSchemaType(schemaType.QualifiedName));
        }
        foreach (XmlSchemaElement schemaElement in xsd.Elements.Values)
        {
            maps.Add(schemaImporter.ImportTypeMapping(schemaElement.QualifiedName));
        }
        foreach (XmlTypeMapping map in maps)
        {
            codeExporter.ExportTypeMapping(map);
        }

        // Check for invalid characters in identifiers
        CodeGenerator.ValidateIdentifiers(codeNamespace);

        // output the C# code
        var codeProvider = new CSharpCodeProvider();

        using (var writer = new StringWriter())
        {
            codeProvider.GenerateCodeFromNamespace(codeNamespace, writer, new CodeGeneratorOptions());
            return writer.GetStringBuilder().ToString();
        }
    }
#>


来源:https://stackoverflow.com/questions/3158646/is-there-t4-templates-available-for-generating-c-sharp-classes-from-xsd

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!