I’ve recently been trying to generate data contracts from xsd files, using svcutil like this:
svcutil.exe /t:code /dconly /out:MyContract.cs /n:*,My.Namespace MyDataDefinition.xsd
The XSD mostly consists of definitions like this:
<xsd:complexType name="SomeComplexObjectType">
<xsd:sequence>
<xsd:element name="FirstData" type="xsd:string" minOccurs="0" />
<xsd:element name="SecondData" type="xsd:string" minOccurs="0" />
</xsd:sequence>
</xsd:complexType>
svcutil generates something like this:
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "3.0.0.0")]
[System.Runtime.Serialization.DataContractAttribute(Name="SomeComplexObjectType")]
public partial class PersonInfo : object, System.Runtime.Serialization.IExtensibleDataObject
{
private string FirstDataField;
private string SecondDataField;
[System.Runtime.Serialization.DataMemberAttribute(EmitDefaultValue=false)]
public string FirstData
{
get
{
return this.FirstDataField;
}
set
{
this.FirstDataField= value;
}
}
[System.Runtime.Serialization.DataMemberAttribute(EmitDefaultValue=false)]
public string SecondData
{
get
{
return this.SecondDataField;
}
set
{
this.SecondDataField= value;
}
}
}
Which works fine, however, the “EmitDefaultValue=false” attributes are not necessary. Not to mention that it introduces a lot of noise into the wsdl, adding stuff like this:
<xsd:element minOccurs="0" name="FirstData" nillable="true" type="xsd:string">
<xsd:annotation>
<xsd:appinfo>
<DefaultValue xmlns="http://schemas.microsoft.com/2003/10/Serialization/" EmitDefaultValue="false"/>
</xsd:appinfo>
</xsd:annotation>
</xsd:element>
Currently I’m just hand-editing the generated contract, but that is not ideal from a maintenance standpoint.
Does anyone know how to prevent svcutil from automatically generating these EmitDefaultValue=false attributes?
Please see this article.
It appears your only choice is to modify the incoming schema so the relevant elements are nillable.