Search code examples
importxsdschemadatacontractserializersvcutil.exe

DataContractSerializer metadata export. Related schemas


I'm exporting assembly's datacontracts with svcutil.exe. It generates set of xsd files including each other like this:

<xs:import namespace="http://schemas.microsoft.com/2003/10/Serialization/" />
  <xs:import namespace="http://schemas.datacontract.org/2004/07/System.Reflection" />
  <xs:import namespace="http://schemas.microsoft.com/2003/10/Serialization/Arrays" />
  <xs:import namespace="http://schemas.datacontract.org/2004/07/System" />
  <xs:import namespace="http://schemas.datacontract.org/2004/07/UBP.Business.Core" />
  <xs:import namespace="http://schemas.datacontract.org/2004/07/UBP.Core" />
  <xs:import namespace="http://schemas.datacontract.org/2004/07/UBP.Collection" />
  <xs:import namespace="http://schemas.datacontract.org/2004/07/System.Data" />

It seems to me that this util supposes that all schemas are pulished on server, where each schema could be accessed by specified hyperlink. But if I open each schema separately in Altova XMLSpy for instance, it will report error that can't find importing schemas. So my question is how to publish generated schemas in order all of them would be visible for each other? Or maybe there are some other options?


Solution

  • The schemas which have been generated are not referenced to each other via hyperlinks, but by relative paths. What you should find is that one of the schema files will be the "main" schema file, and as long as all schema files are present, when you open it in XMLSpy all the relative paths to the other schemas will resolve, and the schema should open fine.

    UPDATE

    My original answer is actually misleading and factually incorrect. There is no relative path defined in the schemas. The files reference each other SOLELY by namespace.

    So a type defined in one schema can be used in another by including a reference to the type's namespace.

    Forget everything else I said (all the relative path stuff was crap and I wasn't thinking straight when I answered it - apologies for that).

    Because you generated these schemas from an assembly which defined lots of interdependent types, the schemas which were generated to contain the XSD equivalents of those types have a high degree of interdependency too, hence all the import statements.

    However, by making reference to these namespaces you are able to author new XSD schemas which make use of the types defined in these generated schemas.

    The namespace you were asking about (http://schemas.microsoft.com/2003/10/Serialization/) does not address a resource; rather it enables the re-use of types which are defined under this namespace. You will probably see a corresponding xmlns attribute in the root of the schema which will define a prefix to use when referencing types from this namespace.

    I hope this is clear.