views:

24

answers:

1

We have an ASP.Net web service which receives an XML file to a PDA and maps it to objects (a standard .Net web method). Is there an easy method of after saving this XML response to disk, having it read back into the same object structure it was in before?

The PDA application automatically does this after receiving the data back from the web service but couldn't find a method that would let me supply a stream or the like to repeat the process 'offline'.

A: 

Serialization allows you to do this - it's actually how objects are sent across a web-service.

If you're lucky, the following code will serialise an object (called "object" of type "object_type".)

XmlSerializer serialiser = new XmlSerializer(typeof(object_type));
FileStream stream = new FileStream(@"C:\Temp\serialised_file.xml", FileMode.Create);
serialiser.Serialize(object, stream);

And to de-serialise:

XmlSerializer serialiser = new XmlSerializer(typeof(object_type));
FileStream stream = new FileStream(@"C:\Temp\serialised_file.xml", FileMode.Open);
object_type object = serialiser.Deserialize(stream) as object_type;

I say "if you're lucky" because 90% of the time that works for me. If you have properties within the class that are abstract classes you may need to declare all class types that extend that abstract class in the XmlSerializer constructor. Also be careful there are no "circular dependencies" within the class.

Andy Shellam