xsd.exe - schema to class - for use with WCF

Neal, besides all the options John has given you, you should also check out the WCSF.blue tool on Codeplex: http://wscfblue.codeplex.com/

It's a "contract first" approach to doing WCF, and one of the many options it offers is to create a WCF DataContract file from your XSD:

alt text

This then pops up a dialog in which you can set a slew of parameters on how to create your C# class file from the XSD:

alt text

Quite useful, even if you want to use it for nothing more than converting XSD to C# classes that work as WCF DataContracts :-)

Also see this blog post for more explanations on the XSD DataContract generation process.


Classes that use [DataContract] will serialize to a very limited schema. For instance, there will be no attributes, only elements. This is intentional, and is done for performance and interoperability.

In general, a schema of the kind you may be working with may be much more complicated. Such a schema will not be usable with the Data Contract Serializer. You will need to use the XML Serializer instead.

In particular, don't edit the classes created by XSD.EXE. In fact, you should never edit generated code, as your edits will be removed as soon as the code is generated again. These classes should already have the attributes on them that will be needed for WCF to work with them.

What happens if you just use them as-is?


I'm shocked that no one pointed me to this utility, which I believe is what I was asking for:

http://msdn.microsoft.com/en-us/library/system.runtime.serialization.xsddatacontractimporter.aspx

I haven't tried it yet, but it looks like XsdDataContractImporter was what I was looking for, or the SVCUTIL with the /dataContractOnly flag.

I learned this when interviewing a candidate yesterday. She said DataContract serialization is faster and would be preferred to use.

Neal

Tags:

C#

Wcf

Xsd

Schema