是否有T4模板可用於從xsd生成c#類?是否有可用於從xsd生成c#類的T4模板?
4
A
回答
3
不是我所知道的,而是看看LINQ to XSD(http://linqtoxsd.codeplex.com/)。您可以使用LinqToXsd.exe根據您的模式生成強類型類。然後你也有完整的LINQ支持。非常便利。
您也可以設置,看起來像在您的項目預生成事件:
"$(ProjectDir)Lib/LinqToXsd/LinqToXsd.Exe" "$(ProjectDir)MySchema.xsd" /fileName:MySchema.cs
這將產生從架構的類你建立正確的之前,所以如果你改變架構中,您的類將與每個構建保持同步。
3
我剛建立了一個非常簡單的應該做的伎倆。
<#@ 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();
}
}
#>
相關問題
- 1. 是否有T4模板從EDMX生成EntityTypeConfiguration?
- 2. 是否有可能使用Qt(不帶XSD工具)從xsd生成C++類?
- 3. 生成類使用T4模板
- 4. 我可以使用T4模板功能從javascript類定義生成C#類嗎?
- 5. 用於生成類型化數據集的T4模板
- 6. 用於爲WP7生成Sql CE類的T4模板
- 7. 實體框架4.3修改用於生成類的T4模板
- 8. Visual Studio 2010用於模板類型的T4代碼生成
- 9. T4模板 - 適合生成C++代碼?
- 10. 從XSD生成C#類(xhtml.blkstruct.class)
- 11. C#從XSD生成類
- 12. 如何使用Fluent Hibernate從T4模板生成LINQ2SQL類
- 13. 使用T4模板生成ViewModels
- 14. 使用模板生成.xaml和.xaml.cs T4
- 15. 用新名稱生成T4模板庫
- 16. 使用T4模板生成代碼
- 17. 我可以得到使用T4模板生成的類繼承相同的類
- 18. 用於PublicResXFileCodeGenerator的T4模板
- 19. 是否可以在t4模板中定義一個新的類?
- 20. 用於生成靈魂文件相對路徑的T4模板
- 21. 是否有可能爲xsd簡單類型生成xml?
- 22. 從C#中的類生成XSD
- 23. C#@枚舉從XSD生成的類
- 24. 用於從xsd生成C++類的JAXB等價物?
- 25. 生成T4模板爲公開
- 26. T4模板和EF與代碼生成
- 27. C#從XSD自動生成類對象
- 28. 從XML,XSD或C#生成LINQ DataContext類
- 29. 使用XSD.exe從多個XSD模式生成c#類
- 30. 是否有任何MVC Razor T4模板可供下載?
不錯,不知道。比預建步驟要好得多。 – 2010-07-01 23:39:21
您也可以在解決方案資源管理器中使用XSD文件的Build Action屬性,並將其設置爲LinqToXsdSchema(這要求您設置項目以導入LinqToXsd.targets) – 2010-07-01 23:41:02