Is there a way to export an XSD schema from a DataContract

Eric Anastas picture Eric Anastas · Apr 9, 2010 · Viewed 9.5k times · Source

I'm using DataContractSerializer to serialize/deserialize my classes to/from XML. Everything works fine, but at some point I'd like to establish a standard schema for the format of these XML files independent of the actual code. That way if something breaks in the serialization process I can always go back and check what the standard schema should be. Or if I do need to modify the schema the modification is an explicit decision rather then just a later affect of modifying my code.

In addition, other people may be writing other software that may not be .NET based that would need to read from these XML files. I'd like to be able to provide them with some kind of documentation of the schema.

Is there some relationship between a DataContract and an XSD schema. Is there a way to export the DataContract attributes in classes as an XSD schema?

Answer

Alex Angas picture Alex Angas · May 2, 2013

Yes, you can use XsdDataContractExporter.

An example is provided in the MSDN article Exporting Schemas from Classes.