是否有T4模板可用于从xsd生成c#类?
是否有T4模板可用于从xsd生成c#类?我不知道,但请看一下LINQ到xsd()。可以使用LinqToXsd.exe根据架构生成强类型类。然后,您还可以获得全面的LINQ支持。非常方便 您还可以在项目上设置预构建事件,该事件类似于:是否有T4模板可用于从xsd生成c#类?,c#,visual-studio,xsd,.net-4.0,t4,C#,Visual Studio,Xsd,.net 4.0,T4,是否有T4模板可用于从xsd生成c#类?我不知道,但请看一下LINQ到xsd()。可以使用LinqToXsd.exe根据架构生成强类型类。然后,您还可以获得全面的LINQ支持。非常方便 您还可以在项目上设置预构建事件,该事件类似于: “$(ProjectDir)Lib/LinqToXsd/LinqToXsd.Exe”“$(ProjectDir)MySchema.xsd”/fileName:MySchema.cs 这将在您构建之前从模式中生成类,因此如果您更改模式,您的类将与每个构建保持同步。我今
“$(ProjectDir)Lib/LinqToXsd/LinqToXsd.Exe”“$(ProjectDir)MySchema.xsd”/fileName:MySchema.cs
这将在您构建之前从模式中生成类,因此如果您更改模式,您的类将与每个构建保持同步。我今天刚刚构建了一个非常简单的类,应该可以做到这一点
<#@ 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();
}
}
#>
不错,我不知道。比预构建步骤好得多。您还可以在解决方案资源管理器中使用XSD文件的build Action属性,并将其设置为LinqToXsdSchema(这要求您将项目设置为导入LinqToXsd.targets)