Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# XML WCF
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 14-Jul-10 1:16am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  

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

  Print Answers RSS
0 BillWoodruff 309
1 CPallini 300
2 Sergey Alexandrovich Kryukov 290
3 George Jonsson 274
4 Gihan Liyanage 230
0 OriginalGriff 4,895
1 CPallini 4,010
2 Sergey Alexandrovich Kryukov 3,514
3 George Jonsson 2,826
4 Gihan Liyanage 2,386


Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 23 Jul 2010
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100