Create XML Serializable Type from Schema (WCF)

Topics: July and December Releases Forum
Jan 22, 2007 at 3:32 PM
I'm running the december CTP and have an XML schema I've managed to generate a DataContract from by using the SvcUtil.exe program.

I belive Aaron Skonnard mentioned that the "Create XML Serializable Type from Schema" recipe would be able to generate a WCF DataContract if the schema followed the constraints set by the DataContractSerializer. But I've tried it a number of times, and that recipe generates the XmlSerializer classes every time.

I guess I'm doing something wrong, running the wrong recipe or it may not be implemented yet. I could swear I saw functions for this in the WSSF source code though...

Thanks for any help.
Jan 22, 2007 at 7:31 PM
Hey Johan, I also left a comment on Aaron's blog, but I'll repeat it here for the benefit of the community.

Check out these links. Hopefully they will shed some light. The first one is a blog entry of mine:

... and shortly thereafter, this became available on MSDN:
Jan 23, 2007 at 8:57 AM
Hi Don,

thanks, I found those pages yesterday when googling around for more info and used the info when crafting the schemas. As I said, SvcUtil manages to create a DataContract from the XSD, but the recipe falls back to an XmlSerializable class.

This is a sample schema I'm working with:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs=""
<xs:complexType name="anteckning">
<xs:element name="id" type="xs:string" nillable="true" maxOccurs="1" minOccurs="0" />

You see anything wrong with it? Thanks for taking time with this.

Jan 24, 2007 at 4:54 PM
Ok, you're right. I was getting confused between 2 of the features. I just tried this and was reminded of what's in there :)

Given only the XSD, only an XML serializable types can be created (using the "Create XML Serializable Type from schema" recipe). The rationale behind this is this: if you only have an XSD, it is VERY unlikely that will be able to be represented as a DataContract (unless you've explicitly created by hand like you've done) because DataContracts implement such a small subset of XSD (which also makes it very interoperable). However, if that XSD exists within a WSDL and you generate the service interface from that WSDL, the recipe will try to create DataContracts. If it can, it will ... if it can't, it will create XML serializable types. This scenario is more likely because the WSDL might have come from a WCF service that uses DataContracts.

Does this make sense?
Jan 24, 2007 at 7:01 PM
Yes, it makes sense Don. Thanks. We (or my friend Eric rather) found the code for the DataContract generation action and created our own recepy for it. We tried the DataContract generation from our own XSD Schemas today and it seems to work like a charm. Sweet stuff! As you said, I'm creating our Schemas with DataContract generation in mind, but even more complex schemas seems to work perfect as long as you adhere to "the rules".

I'll ask Eric to explain what he did if someone else wants to do the same.
Jan 25, 2007 at 7:02 PM
Oh, that's cool. You know ... if you wanted to share. I'd be happy to take what Eric has done and post it to this community so others could use it.

Maybe in a "how to modify the WCF guidance package" video, I could illustrate how to hook it up. Just a thought.

Thanks Johan,

Apr 23, 2007 at 8:12 PM
I send you an email with the code on the 28th february. I guess that you get a lot of emails, so I just wanted to notify that it's there :)
Apr 24, 2007 at 1:56 AM
Hi Eric, as Don said, you can send me the code and I will post it here Community Contributions page (read at the top on publishing code) to share with all the community.
Thanks and we encourage all members who want to share any enhancement/update for WSSF.