views:

1227

answers:

4

I generated some C# classes from an XSD using the Visual Studio XSD utility and it generated arrays for storing a collection of elements, rather than one of the built-in generic Collection<T> (or related) classes.

None of the command line parameters mentioned in xsd /? mention anything about generating collections rather than arrays, but I know that this can be done with web service proxy classes that Visual Studio generates, so I figured it must be possible.

Does anybody know how to have the XSD utility generate collection classes rather than arrays?

A: 

The problem is that XSD is XML - language agnostic. It can't use anything from Java or C# that the other doesn't support. Arrays are the lowest common denominator.

Java developers use OXM libraries like XStream to marshal objects to XML and back. It allows you to specify custom types when going from objects to XML. Sounds like you'll have to find something similar in the .NET toolbox.

This isn't an answer to your question, but it's the reason why I think XSD won't do the job. Like a Java developer reaching out to XStream, you'll have to look elsewhere.

duffymo
+1  A: 

You've seen the ability to use collections with Service References, not with Web References. Look at the svcutil.exe utility instead. It looks like svcutil /t:code file.xsd might do this for you.

John Saunders
A: 

I am not aware of any tools out there at this time that do this reliably, unfortunately.

There was an XSDObjectGen (or something like that) but that tool seems to have vanished from the face of the cyberspace :-(

I saw one approach use XSDTidy to apply a sort of a replacement of all int[] into List over the output of the XSD tool - seems to have worked, but seemed a bit clunky.

Here's a CodeProject article on XSDTidy.

The thing I've been trying to find again is a blog post by a gentleman who basically dissected the inner workings of XSD in Reflector, and then used the knowledge he gained on that to build his own tool, using a lot of .NET core classes. If I remember correctly, it was this blog post here ("writing your own xsd.exe"). Mike isn't exactly doing what you're looking for (creating List instead of arrays), but it could be a starting point.

Marc

marc_s
+3  A: 

Found this post when searching for an answer. I'm using this tool: http://xsd2code.codeplex.com/

chandra