views:

1231

answers:

5

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.

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?

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

A: 

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.

Artem K.
A: 

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.

Joe Caffeine
+1  A: 

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.

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.

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.

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.

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.

But it is possible.

Good luck!

Leon Breedt
A: 

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.

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

Simon_Weaver