generate data class from XSD to use between wcf service and client?
I have used xsd.exe tool to generate data class but wcf doesn't like the data class to be exchanged and throws an error
"There was an error while trying to serialize parameter http://tempuri.org/:orderItems. The InnerException message was 'Type 'Store.Schemas.Order' with data contract name 'Order:http://schemas.datacontract.org/2004/07/Store.Schemas' is not expected. Add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.'. Please see InnerException for more details. "
There are a lot of classes that comes based of XSD. As per MSDN , it says http://msdn.microsoft.com/en-us/library/ms751512.aspx , every data classes to be decorated with KnownTypeAttribute,DataContractAttribute, DataMemberAttribute etc.
Is there a way where I can generate data class from xsd that can be used with WCF service? because changing the class generated by XSD.exe is not recommended nor easy process.
i guess the first question is that do you have the wsdl as well which is referring the xsd. if yes then you can use the scvutil.exe and pass the wsdl path and make sure that the xsd files are also in same directory as wsdl file. this will generate the required data classes for you.
HTH
If you want to generate classes using XSD.exe you will get XML serializable data type marked with all these Xml attributes conrolling its serialisation by XmlSerializer
. Wcf by default uses DataContractSerializer
which doesn't use those attributes used to describe serialization. You must switch to XmlSerializer
by using XmlSerializerFormat
attribute on your service, operation or service contract.
http://wscfblue.codeplex.com/开源生成器解决了这个问题
链接地址: http://www.djcxy.com/p/34298.html上一篇: svcutil.exe无法处理导入?