4

The JSON script serializer (from mvc) can serialize an anonymous class to script. Are there some standard framework (or mvc) classes that can transform an anonymous class to xml?

Currently anonymous classes are not marked as serializable and cannot be converted to xml.

Is there a way to do this without writing reams of code?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Jim
  • 14,952
  • 15
  • 80
  • 167

2 Answers2

3

You need reams of code.

But others have already done it for you.

Here's a jQuery plugin (client-side): http://plugins.jquery.com/project/json2xml

Here's some C# code: http://www.phdcc.com/xml2json.htm

101010
  • 14,866
  • 30
  • 95
  • 172
0

I think that the above did not answer the question directly. Jim did not ask about JSON to XML serializer, but C# anonymous type to XML serializer. This class does not exist out of a box, but can be written easily. Look here: Can I serialize Anonymous Types as xml?

Community
  • 1
  • 1
Habdank
  • 183
  • 1
  • 5
  • The answer 'You need reams of code' is correct. The answer you have highlighted provides a very simple class -> xml producer using reflection. Unfortunately a truly useful implementation will need a lot more code. it has to cater for enums, arrays, enumerables, and references to other serializable types. It's a big job to do it properly. – Jim Sep 04 '12 at 04:29