Developer
Developer

Reputation: 18699

XML to .NET Classes

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?

Upvotes: 2

Views: 238

Answers (6)

Matthew Whited
Matthew Whited

Reputation: 22443

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

Upvotes: 1

Mitchel Sellers
Mitchel Sellers

Reputation: 63136

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.

Upvotes: 1

JohnOpincar
JohnOpincar

Reputation: 5823

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

Upvotes: 1

Reed Copsey
Reed Copsey

Reputation: 564881

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

Upvotes: 0

Lars Fastrup
Lars Fastrup

Reputation: 5506

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.

Upvotes: 2

Anton Gogolev
Anton Gogolev

Reputation: 115877

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.

Upvotes: 2

Related Questions