Wednesday, 1 April 2009

Working with web services - XML serialisation

As before I've got a quote object that is populated and passed across to a third party. It was turning into a real biatch of a task mainly due to some bad error messaging from a third party web service.

At this point, something I found really useful was to look at a copy of the XML that was being sent to the web service. Getting into the XML file allowed me to really interrogate the data and it highlighted some formatting errors that had been missed by the unit tests - mainly date and currency formatting but enough to cause the web service to reject the request.

If you find yourself in a similar dilemma use the code below and switch out the object type, file location and variable names for your own:

var request = new QuoteRequest();

request = BuildRequest();

FileStream fileStream = new FileStream(@"c:\temp\example_request.xml", FileMode.OpenOrCreate);
TextWriter stream = new StreamWriter(fileStream);
XmlSerializer xmlSerializer = new XmlSerializer(typeof(
xmlSerializer.Serialize(stream, request);