在DataContractSerializer之前有XMLSerializer, 网上关于此的比较有:
亮点在此: 10%的性能提升.
因为DataContractSerializer不支持一些xsd标签:
group, all, choice...
http://msdn.microsoft.com/zh-cn/library/ms733112.aspx
如果要使用DataContractSerializer就需要将XSD转为符合其标准的XSD. 有以下步骤:
1. convert xsd to a standard xsd.
2. use xsd2code to generate cs file.
3. add the cs file into a assembly project and build to a dll file.
4. use svcutil to generate xsd file via the dll, command: svcutil.exe /t:metadata /dataContractOnly skd.dll
5. remove all ...specified element. since xsd2code will generate a ...specified element for value type element if its optional is true, to indicate the value was set or not.
like:
int Age{get;set;}
int AgeSpecified{get;set}
if AgeSpecified==false, even if we set Age=20, XMLserialize will also ignore Age, and Age not be serialized.
but in step 4, the below element will be added into the Schame:
<xs:element minOccurs="0" name="AgeSpecified" type="xs:boolean" />
this addtional elements cost performance and let schame not very clear, that's why we need step5.
xsl:
<xsl:template match="xs:element[substring(@name,string-length(@name)-8,9)='Specified']" priority="1"></xsl:template>
<xsl:template match="xs:element[(@minOccurs='0') and not (@nillable='true')]">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
<xsl:attribute name="nillable">true</xsl:attribute>
</xsl:copy>
Notice, we added nillable=true attribute on all value-type elements, let svcutil to generate nullable property, to represent the state of specified =false.
6. add emit default info, let svcutil generate the property with [System.Runtime.Serialization.DataMemberAttribute(EmitDefaultValue=false)]
then DataContractSerializer will naver serialize the property when its value is default.
http://msdn.microsoft.com/en-us/library/aa347792.aspx
http://kb.cnblogs.com/a/1520121/
XSL:
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
<xs:annotation>
<xs:appinfo>
<DefaultValue EmitDefaultValue="false" xmlns="http://schemas.microsoft.com/2003/10/Serialization/" />
</xs:appinfo>
</xs:annotation>
</xsl:copy>
7. generate the cs file by xsd, use command svcutil ddkkd.cs /dconly *.xsd