生成已知的XSD .NET对象 [英] Generate .Net objects from known XSD

查看:109
本文介绍了生成已知的XSD .NET对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些XSD-S定义我的对象层次结构。例如math.xsd,base.xsd 而math.xsd是取决于base.xsd。我需要从这些XSD-S生成的类。

I have some XSD-s that define my objects hierarchy. for example math.xsd, base.xsd while math.xsd is depends on base.xsd. I need to generate classes from those xsd-s.

我已经了解这两个工具: codeXS 工具和的 XSD.EXE 。 与XSD.EXE的问题是,我没有从两个XSD-S依赖于对方成功生成的类。有没有谁知道正确的参数使用XSD.EXE这种情况?

I've already read about those two tools: CodeXS tool and XSD.exe. the problem with the xsd.exe is that I didn't succeed generating classes from two xsd-s that depends on each other. is there anyone who knows the right parameters for using the xsd.exe for such case?

此外,我要找:   - 更多的工具   - 这些工具之间的比较   - XSD使用.NET 3.5对象 谢谢你。

moreover, I am looking for: - more tools - comparison between those tools - xsd to object using .net 3.5 Thanks.

推荐答案

有没有理由,你不能使用相同的方法 XSD.EXE 使用,但随后运行对产生的codeDOM模型自己的code键使你写出的.cs文件到磁盘之前,需要修改。

There's no reason you couldn't use the same approach xsd.exe uses, but then run your own code against the generated CodeDOM model to make the modifications you need before writing out the .cs files to disk.

总的想法是,你加载XSD文件到的XmlSchema 对象,然后使用内部 XML codeExporter XmlSchemaImporter 类填充codeDOM命名空间。

The general idea is that you load your XSD file into an XmlSchema object, then use the internal XmlCodeExporter and XmlSchemaImporter classes to populate a CodeDOM namespace.

在你这样做,你可以自由地做你需要在codeDOM AST任​​何调整,然后写入到磁盘上。

After you've done that, you're free to make any tweaks you need to make against the CodeDOM AST, and then write it out to disk.

例如。

  XmlSchema schema = null; // Load XSD file here
  var schemas = new XmlSchemas();
  schemas.Add(schema);

  var ns = new CodeNamespace { Name = "MyNamespace" };

  ns.Imports.Add(new CodeNamespaceImport("System"));
  ns.Imports.Add(new CodeNamespaceImport("System.Collections.Generic"));

  var exporter = new XmlCodeExporter(ns); 
  var importer = new XmlSchemaImporter(schemas); 

  foreach (XmlSchemaElement element in schema.Elements.Values) 
  { 
    var mapping = importer.ImportTypeMapping(element.QualifiedName);
    exporter.ExportTypeMapping(mapping); 
  }

  // Transform CodeDOM as required, adding new attributes, methods, modifying
  // inheritance hierarchy, whatever.

  var provider = new CSharpCodeProvider(); 
  using (var writer = new StreamWriter(outputFile, false))
    provider.GenerateCodeFromNamespace(ns, writer, new CodeGeneratorOptions())

如果您的架构中引用其他模式,你将不得不使用的XmlSchemaSet ,并设定的XmlResolver 属性你写一个解析器,它会发现导入的模式,并将其提供给的XmlSchemaSet 当你调用编译()就可以了。

If your schemas reference other schemas, you'll have to use XmlSchemaSet, and set the XmlResolver property to a resolver that you write, which will find the imported schemas and provide them to the XmlSchemaSet when you call Compile() on it.

有可能导入的模式在不同的命名空间声明的事情,如果你希望你的的XmlSerializer 生成XML在一个不同的命名空间中导入的项目,你可以要破解产生codeDOM公平一点。

It is possible for imported schemas to declare things in a different namespace, and if you want your XmlSerializer to generate XML with the imported items in a different namespace, you may have to hack the generated CodeDOM a fair bit.

但是,有可能

祝你好运!

这篇关于生成已知的XSD .NET对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆