0

We have a pretty lengthy xsd and we generated c# DTO class using xsd.exe. We need to mark our classes as DataContract and its memebers as DataMember as we we want to use DataContractSerializer than XML.

I was able to mark classes and memebers appropriatley but while trying to run code, I am getting below KnownType exception, I tried adding all KnownType on all the classes but the errors are on similar lines. I haven't been able to find any solution for this. Can some one please suggest that how do we use DataContract on the file generated through xsd.exe.

Consider using a DataContractResolver if you are using DataContractSerializer or 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 the serializer.System.ServiceModel.CommunicationException:

I tired svcutil and it gives me tons of errors and also doesn't generate output as expected while XSD does.

Punit
  • 1,347
  • 3
  • 20
  • 39
  • 1
    Don't use `xsd.exe` use `svcutil.exe` which is designed to auto-generate data contract classes. See [Generate DataContract from XSD](https://stackoverflow.com/q/2188909/344280). – dbc Dec 04 '18 at 22:36
  • Possible duplicate of [Generate DataContract from XSD](https://stackoverflow.com/questions/2188909/generate-datacontract-from-xsd) –  Dec 04 '18 at 22:39
  • Thanks @MickyD ..I tried Svcutil as well but it doesn't generate properly and hence I have to stuck with xsd. – Punit Dec 04 '18 at 22:42
  • Well then possibly your XSD uses XML & XSD features not supported by the data contract serializer, such as attributes, collections without an outer container element, or polymorphism via element name rather than via `xsi:type` attribute. So I think we need to see a [mcve] to help you, ideally an example of some XSD files for which wrong classes are generated by `svcutil.exe`, plus XML they fail to deserialize. – dbc Dec 04 '18 at 22:45
  • Sorry to hear that Punit. I agree with dbc that you need to most a MCVE. Any chance you can post some of the XSD that is giving you grief? –  Dec 05 '18 at 01:02
  • @MickyD - XSD is pretty big, it is MISMO 3.4 which is around 8 mb so won't be able to paste i guess...I didn't knew that DataContractSerializer doesn't support few xml attributes that might be a reason... Thank you for that info dbc .... Looks like only way to use is XMLSerializer to move forward.... – Punit Dec 05 '18 at 03:14
  • We said _"minimal"_. Surely you don't have problems with the entire XSD? –  Dec 05 '18 at 09:12

0 Answers0