4

I'd like to generate the classes the xsd.exe way and not the linqtoxsd way.

7
  • 1
    Why wouldn't you use xsd.exe? Commented Jul 1, 2009 at 9:52
  • Perhaps does not want to, what is it to you? Commented Jul 1, 2009 at 9:54
  • 1
    @leppie: it would clarify the requirements. For instance, does he have a restriction to not call external programs? To not use Microsoft programs? Commented Jul 1, 2009 at 9:56
  • @leppie: wouldn't the simplest solution be to call xsd? One line of code? (KISS/you ain't gonna need it) Commented Jul 1, 2009 at 10:11
  • One of the reason against XSD is that is STILL to this day doesn't support List<T> - it still always creates T[] which is just plain horrible. Commented Jul 1, 2009 at 11:12

4 Answers 4

10

Look at the System.Xml.Serialization.XmlCodeExporter class.

UPDATE (in case John Saunders didnt bother reading further)

"Xsd.exe uses XmlCodeExporter to generate classes from XML Schema Definition (XSD) documents."

Sign up to request clarification or add additional context in comments.

10 Comments

"This API supports the .NET Framework infrastructure and is not intended to be used directly from your code"
I been using it since 1.1, is there a problem with it?
"is not intended to be used" doesn't mean "cannot be used" ;)
+1 for that info. I always thought launching xsd.exe in a process was the only solution. :-)
@leppie,Thomas: it depends on whether this code is important to you. "infrastructure" code is subject to change without whining. After all, they've only told you not to use it since .net 1.1.
|
5

Try Xsd2Code

Seems to be the best free/Open source tool out there.

Good integration with VS2010

It's working great for me.

Comments

3

You can call xsd.exe from your code using Process Class. I did it once and its fast and straightforward. You won't need to bored much :).

Comments

1

Which part of the process do you not know how to do?

You can load one or more instances of the XmlSchema class into an XmlSchemaSet. This will allow you to programmaticly examine all parts of the schema and other schemas it may reference. You can use this information to generate code, either as text that will later need to be compiled, or else using the CodeDOM. See Dynamic Source Code Generation and Compilation.


If you are generating code in the context of Visual Studio, then I'd suggest you do so via T4 Templates. See Generating Artifacts By Using Text Templates.

4 Comments

Due to the complexity of XML Schema, parsing it myself and generating the classes from it would be a nightmare, to say the least.
I didn't suggest you parse it yourself. You load a single schema by using XmlSchema.Load, then add the result to an XmlSchemaSet. Add all the schemas you need to that set, then you can "compile" it to validate it and fix cross-references; finally, you can walk the structure and generate your code.
I'm sorry, I meant to say walking (not parsing) the structure. I don't know how much you're familiar with the SOM or the XML Schema spec. but it's a big and rather complicated task to generate code from an XML Schema (even when considering that you should support only a subset of the XML Schema spec. that could actually be translateable to code straight forward) this is why I prefer to re-use code for that matter.
I'm very familiar with the SOM, which is why my choice would have been to generate code based on T4 templates driven from the SOM.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.