XML serialization options in .NET
Posted
by Borek
on Stack Overflow
See other posts from Stack Overflow
or by Borek
Published on 2010-05-11T10:08:58Z
Indexed on
2010/05/11
11:54 UTC
Read the original article
Hit count: 237
I'm building a service that returns an XML (no SOAP, no ATOM, just plain old XML). Say that I have my domain objects already filled with data and just need to transform them to the XML format. What options do I have on .NET?
Requirements:
- The transformation is not 1:1. Say that I have an Address property of type Address with nested properties like Line1, City, Postcode etc. This may need to result in an XML like
<xaddr city="...">Line1, Postcode</xaddr>
, i.e. quite different. - Some XML elements/attributes are conditional, for example, if a Customer is under 18, the XML needs to contain some additional information.
- I only need to serialize the objects to XML, the other direction (XML to objects) is not important
- Some technologies, i.e. Data Contracts use .NET attributes. Other means of configuration (external XML config, buddy classes etc.) would be a plus.
Here are the options as I see them as the moment. Corrections / additions will be very welcome.
- String concatenation - forget it, it was a joke :)
- Linq 2 XML - complete control but quite a lot of hand written code, would need good suite of unit tests
- View engines in ASP.NET MVC (or even Web Forms theoretically), the logic being in controllers. It's a question how to structure it, I can have simple rules engine in my controller(s) and one view template per each possible output, or have the decision logic directly in the template. Both have upsides and downsides.
- XML Serialization - I'm not sure about the flexibility here
- Data Contracts from WCF - not sure about the flexibility either, plus would they work in a simple ASP.NET MVC app (non-WCF service)? Are they a super-set of the standard XML serialization now?
- If it exists, some XML-to-object mapper. The more I think about it the more I think I'm looking for something like this but I couldn't find anything appropriate.
Any comments / other options?
© Stack Overflow or respective owner