tags:

views:

117

answers:

6

I am designing a WCF service which have some operations that i expose to get some data. These operations accept a xml document as a parameter. Is there a way that i can map the nodes of this document to the classes i have created to be used internally?

EDIT: One of the reasons i accept a xml document rather than a strongly typed object is that we can extend the api in the future without having to worry about breaking the structure for the developers who are using the older version. Is there a better way to design api's that can evolve without having to ask the clients to change their existing implementations?

+2  A: 

If you have a schema (XSD) for the XML document you're using, you can try xsd.exe which ships with .NET SDK, which will generate classes for that schema.

Anton Gogolev
+2  A: 

Yes, you can use the XmlSerializer class to convert XML to objects and vice versa. It is really cool and easy to use as it simply uses .NET reflection to find the public properties on the class that map to XML elements. The exact XML format can be controlled using attributes like the XmlElementAttribute.

Lars Fastrup
A: 

You can use an XMLReader directly or use LINQ to XML to map the document to your classes.

Reed Copsey
+1  A: 

Why not define the service interface using the classes that you will map to internally? Then you don't have to do any mapping.

JohnOpincar
Excellent point...
Hemant
I do not want to expose the classes as if i were to change the properties or types in the classes, then the clients consuming the service would have to modify their code as well. Also based on the limited set of values that are sent through xml, the service has custom logic to fill out other properties..
Nick
I would still use classes. I understand that you want to isolate the internal classes from the public API. But I still use classes in those cases -- just another set of classes with mapping code (which can leverage reflection and "auto" property mapping). You can use nullable types for optional values.
JohnOpincar
+1  A: 

You can also create objects, and work with the Serialize/Deserialize functions to build a class manually that matches the structure that you need.

It is actually a very easy process, this MSDN article http://msdn.microsoft.com/en-us/library/ms950721.aspx, goes a long way to introduce the concept.

Mitchel Sellers
+1  A: 

I know you want XML and for that I would recommend the XMLSerializer from your .Net Classes. But if you are only concerned with versioning you may look at the Service Versioning support in WCF

http://msdn.microsoft.com/en-us/library/ms731060.aspx

Matthew Whited