Search code examples
c#wcfxsddatacontractdatacontractserializer

generate datacontract using svcutil.. how do I handle attributes?


A number of XSD's were provided to me along with sample xml. A snippet of one sample xml is:

<tfsChequeId xmlns="http://www.something.com/XMLSchemas/itrs/tfs/v1">
        <dic numericCode="20010411199194813505"/>
    </tfsChequeId>

The xsd for this snippet is as follows:

<xsd:element name="tfsChequeId" type="tfs:TfsChequeId" minOccurs="1" maxOccurs="1" />

<xsd:complexType name="TfsChequeId">
<xsd:sequence>
<xsd:element name="dic" type="tfs:TfsChequeIdDic" minOccurs="0" maxOccurs="1">
<xsd:annotation>
<xsd:documentation>The Document Identity Code (Doc-ID Code / DIC) is the representation of the document identity as printed on the document (usually also as barcode).
In many cases it also contains additional information for the form (refund rule, check digit, etc.). Several variants of the document identity code exist, but
they all contain at least the information that is included in the document identity number (country, store and serial number).
</xsd:documentation>
</xsd:annotation>
</xsd:element>
</xsd:sequence>
</xsd:complexType>

    <xsd:complexType name="TfsChequeIdDic">
<xsd:attribute name="numericCode" type="common:DocumentIdentifierCode">
<xsd:annotation>
<xsd:documentation>The document identity code is the representation of the document identity as printed on the document (usually also as barcode).
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:complexType>

The xsd's are quite complex. On generating the datacontracts using svcutil.exe a number of errors were generated.. All of the following form:

Error: There was a validation error in the schemas provided for code generation:

Source:
Line: 85 Column: 6


Validation Error: Type 'http://www.something.com/XMLSchemas/itrs/common/v1:Docu
mentIdentifierCode' is not declared, or is not a simple type.

The generated datacontract is as follows:

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
[System.Runtime.Serialization.DataContractAttribute(Name="TfsChequeId", Namespace="http://www.something.com/XMLSchemas/itrs/tfs/v1")]
public partial class TfsChequeId : object, System.Runtime.Serialization.IExtensibleDataObject
{

    private System.Runtime.Serialization.ExtensionDataObject extensionDataField;

    private www.something.com.XMLSchemas.itrs.tfs.v1.TfsChequeIdDic dicField;

    public System.Runtime.Serialization.ExtensionDataObject ExtensionData
    {
        get
        {
            return this.extensionDataField;
        }
        set
        {
            this.extensionDataField = value;
        }
    }

    [System.Runtime.Serialization.DataMemberAttribute(EmitDefaultValue=false)]
    public www.something.com.XMLSchemas.itrs.tfs.v1.TfsChequeIdDic dic
    {
        get
        {
            return this.dicField;
        }
        set
        {
            this.dicField = value;
        }
    }
}

However I've no idea how to use this.. ie set numericCode? Any ideas/hints/tips would be gratefully received.

Fiona


Solution

  • First thing you need to do is to ensure you can run your svcutil without an error. I suspect your DocumentIdentifierCode is defined in an XSD that wasn't included in your command line. Please take a look at this post on how to refactor the XSD if it is really complicated. If you don't have that many files, then use a command line similar to the one shown in this post: replace /mc with /dconly (for data contract only) but make sure you list all the XSDs. I've seen cases where svcutil would still not work, in which case try the xsd.exe command line (see the first link I've referenced).