从已知的XSD生成.Net对象

时间:2022-01-26 17:19:20

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.

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

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?

我已经读过这两个工具:CodeXS工具和XSD.exe。 xsd.exe的问题是我没有成功从两个相互依赖的xsd-s生成类。是否有人知道使用xsd.exe这种情况的正确参数?

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

此外,我正在寻找: - 更多工具 - 这些工具之间的比较 - 使用.net 3.5对象的xsd谢谢。

5 个解决方案

#1


3  

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.exe使用的相同方法,但是然后针对生成的CodeDOM模型运行自己的代码,以便在将.cs文件写入磁盘之前进行所需的修改。

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.

一般的想法是您将XSD文件加载到XmlSchema对象中,然后使用内部XmlCodeExporter和XmlSchemaImporter类来填充CodeDOM名称空间。

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.

完成后,您可以*地对CodeDOM AST进行任何调整,然后将其写入磁盘。

Eg.

  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())

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.

如果您的模式引用其他模式,则必须使用XmlSchemaSet,并将XmlResolver属性设置为您编写的解析程序,这将在您调用Compile()时找到导入的模式并将它们提供给XmlSchemaSet。

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.

导入的模式可以在不同的命名空间中声明事物,如果您希望XmlSerializer使用不同命名空间中的导入项生成XML,则可能需要对生成的CodeDOM进行一些修改。

But it is possible.

但这是可能的。

Good luck!

#2


2  

Microsoft: XSDObjectGen - Sample Code Generator 1.4.2.1
*: XSDObjectGen.exe vs XSD.exe

Microsoft:XSDObjectGen - 示例代码生成器1.4.2.1 *:XSDObjectGen.exe与XSD.exe

#3


1  

its an [expensive] commercial product, and i cannot vouch for it one way or the other but another possibility is http://www.liquid-technologies.com/. Their 'xsd-gen' tool supports many languages such as Java/C#/Silverlight and even C++ ! Worth a look if you need to support two languages consistently.

它是一种[昂贵的]商业产品,我不能以某种方式担保它,但另一种可能性是http://www.liquid-technologies.com/。他们的'xsd-gen'工具支持许多语言,如Java / C#/ Silverlight甚至C ++!值得一看,如果您需要始终如一地支持两种语言。

Note: they have a community edition but that has no code-gen capabilities.

注意:他们有社区版,但没有代码生成功能。

#4


0  

Have you tried the LINQ to XSD? The project name is not really describes it's goal, so I should tell that it is useful replacement to xsd.exe.

您是否尝试过LINQ to XSD?项目名称并没有真正描述它的目标,所以我应该告诉它它是xsd.exe的有用替代品。

#5


0  

I had a project a while back that involved turning DTD documents into XSD documents and then into .Net classes. XSD.exe turned out uselness code since there were a number of XSD documents which refered to a common base XSD. I ended up writing my own XSD to .Net generator using a combination of reflection to manipulate the internals of system.xml and by reverse engineering xsd.exe. It took me about 2 weeks to hack together a viable generator.

我不久前有一个项目涉及将DTD文档转换为XSD文档,然后转换为.Net类。 XSD.exe编写了uselness代码,因为有许多XSD文档引用了公共基础XSD。我最终使用反射的组合来编写我自己的XSD到.Net生成器,以操纵system.xml的内部和反向工程xsd.exe。我花了大约两个星期的时间来组合一个可行的发电机。

#1


3  

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.exe使用的相同方法,但是然后针对生成的CodeDOM模型运行自己的代码,以便在将.cs文件写入磁盘之前进行所需的修改。

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.

一般的想法是您将XSD文件加载到XmlSchema对象中,然后使用内部XmlCodeExporter和XmlSchemaImporter类来填充CodeDOM名称空间。

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.

完成后,您可以*地对CodeDOM AST进行任何调整,然后将其写入磁盘。

Eg.

  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())

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.

如果您的模式引用其他模式,则必须使用XmlSchemaSet,并将XmlResolver属性设置为您编写的解析程序,这将在您调用Compile()时找到导入的模式并将它们提供给XmlSchemaSet。

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.

导入的模式可以在不同的命名空间中声明事物,如果您希望XmlSerializer使用不同命名空间中的导入项生成XML,则可能需要对生成的CodeDOM进行一些修改。

But it is possible.

但这是可能的。

Good luck!

#2


2  

Microsoft: XSDObjectGen - Sample Code Generator 1.4.2.1
*: XSDObjectGen.exe vs XSD.exe

Microsoft:XSDObjectGen - 示例代码生成器1.4.2.1 *:XSDObjectGen.exe与XSD.exe

#3


1  

its an [expensive] commercial product, and i cannot vouch for it one way or the other but another possibility is http://www.liquid-technologies.com/. Their 'xsd-gen' tool supports many languages such as Java/C#/Silverlight and even C++ ! Worth a look if you need to support two languages consistently.

它是一种[昂贵的]商业产品,我不能以某种方式担保它,但另一种可能性是http://www.liquid-technologies.com/。他们的'xsd-gen'工具支持许多语言,如Java / C#/ Silverlight甚至C ++!值得一看,如果您需要始终如一地支持两种语言。

Note: they have a community edition but that has no code-gen capabilities.

注意:他们有社区版,但没有代码生成功能。

#4


0  

Have you tried the LINQ to XSD? The project name is not really describes it's goal, so I should tell that it is useful replacement to xsd.exe.

您是否尝试过LINQ to XSD?项目名称并没有真正描述它的目标,所以我应该告诉它它是xsd.exe的有用替代品。

#5


0  

I had a project a while back that involved turning DTD documents into XSD documents and then into .Net classes. XSD.exe turned out uselness code since there were a number of XSD documents which refered to a common base XSD. I ended up writing my own XSD to .Net generator using a combination of reflection to manipulate the internals of system.xml and by reverse engineering xsd.exe. It took me about 2 weeks to hack together a viable generator.

我不久前有一个项目涉及将DTD文档转换为XSD文档,然后转换为.Net类。 XSD.exe编写了uselness代码,因为有许多XSD文档引用了公共基础XSD。我最终使用反射的组合来编写我自己的XSD到.Net生成器,以操纵system.xml的内部和反向工程xsd.exe。我花了大约两个星期的时间来组合一个可行的发电机。