Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
Hi!

Is it possible to use both DataContractSerializer and XmlSerializer (combine them) in a WCF-service, and maybe also in one method?

I have a project with approximately 50 classes marked with DataContract-attributes. I altso have some external XSD-schemas I have to use wich is not compatible with DataContractSerializer (collections are not grouped in a tag). I have generated classes (another 50) marked with XmlSerializer-attributes from these schemas. My solution looks similar to this:

Classes marked as DataContracts:
- Driver
- Wheel
- ConnectionInfo

Classes marked as XmlSerializable:
- StandarizedCar
- MyCustomCar : StandarizedCar

So, Standarized car is generated from XSD and marked with XmlSerializable-attributes. I made a class MyCustomCar wich extends StandarizedCar and adds some properties (like wheels and a driver, notice that Wheel is a DataContract). I want to make a service with the following methods:

void:Connect(ConnectionInfo info)
MyCustomCar:GetCustomCar(int carID)
void:AddWheel(Wheel wheel)


My question(s):

1. Can my Service use both DataContractSerializer and XmlSerializer?
2. Can my method GetCustomCar() use both DataContractSerializer and XmlSerializer, or do I have to choose one for each method?
3. Can I mark Wheel with both DataContractSerializer-attributes AND XmlSerializer attributes?
4. Will GetCustomCar work if I mark it with [XmlSerializerFormat] even though Wheels don't have any XmlSerializer-attributes)?
Posted

1 solution

1. Can my Service use both DataContractSerializer and XmlSerializer?
Yes- but the WCF client expects the DataContractSerializer to be used.

2. Can my method GetCustomCar() use both DataContractSerializer and XmlSerializer, or do I have to choose one for each method?
I'm not sure why you'd want to,but you'd have to work out which is the correct one to run at runtime, if you can't do this then you can't used the same method


3. Can I mark Wheel with both DataContractSerializer-attributes AND XmlSerializer attributes?
Yes, AFAIK

4. Will GetCustomCar work if I mark it with [XmlSerializerFormat] even though Wheels don't have any XmlSerializer-attributes)?
Yes: It will just serialize the properties in the super class you decoracted.

I'm not 100% about all this, so the best thing to do is cut code. I'd add this, if you are using the XmlSerializer for server-side persistence and the DataContract Serialzer for WCF service communication, a MUCH better architecture wild be to have a DataContract-based obect --> XmlSerialer-based object converter (two), then you separate the two layers out completely. As your classes are similar, you can probably write so code to auto-generate the classes (and converters) for you.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900