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

后端 未结 3 696
清酒与你
清酒与你 2021-01-02 08:32

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

相关标签:
3条回答
  • 2021-01-02 08:46

    You might be able to generate schema files from DataContracts using the svcutil.exe tool that comes with Visual Studio.

    svcutil myAssembly.dll
       - Generate metadata documents for Service Contracts and associated types in an assembly
    
    svcutil myServiceHost.exe /serviceName:myServiceName
       - Generate metadata documents for a service, and all associated Service Contracts and data types in an assembly
    
    svcutil myServiceHost.exe /dconly
       - Generate metadata documents for data types in an assembly
    

    I believe I messed with this at one point in the past, and may or may not have gotten it to work. Another easy way to generate schemas is to stand up a WCF service that uses your data contracts, and access the WSDL. The WSDL will import all the xsds for the DataContracts.

    0 讨论(0)
  • 2021-01-02 08:52

    Yes, you can use XsdDataContractExporter.

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

    0 讨论(0)
  • 2021-01-02 08:57

    Svcutil.exe can "export metadata for compiled data contracts".
    There is relationship between DataContract and XSD:

    The DataContractSerializer maps CLR types to XSD when metadata is exported from a Windows Communication Foundation (WCF) service using a metadata endpoint or the ServiceModel Metadata Utility Tool (Svcutil.exe). For more information, see Data Contract Serializer.

    The DataContractSerializer also maps XSD to CLR types when Svcutil.exe is used to access Web Services Description Language (WSDL) or XSD documents and generate data contracts for services or clients.

    You can get the XSD(s) at run-time as well, even in your browser, by setting up a MEX endpoint.
    The WSDL by default will contain references to XSD(s) that can be accessed through the endpoint as well.

    0 讨论(0)
提交回复
热议问题