Serialize XML to a string

I am trying to serialize a class into XML that can be stored to a string. I need this to be compatible with iOS, web browser, and flash, which means it can’t use reflection to serialize. How can I do this? I need it to be put into a string because I will be sending it to a server, instead of saving anything locally (since the web browser doesn’t allow for this).

Thanks for the help!

Firstly you are quite able to use reflection on iOS and web so long as on the web you don’t do much fiddling with private fields.

You can use BinaryFormatter and Convert to create a string faster and smaller than XML.

Like this to create a compressed string:

  var m = new MemoryStream();
  var b = new BinaryFormatter();
  b.Serialize(m, yourObject);
  var s = Convert.ToBase64String(m.GetBuffer());

Like this to decompress it:

  var m = new MemoryStream(Convert.FromBase64String(dataYouReceived));
  var b = new BinaryFormatter();
  var o = (YourObject)b.Deserialize(m);

You’ll need System.IO and System.Runtime.Serialization.Binary