I am receiving an XML via an HttpPost to my service, and I want to log the received value for debugging purposes.
First I deserialize it into an entity like this:
XmlSerializer s = new XmlSerializer(typeof(TransactionResults));
TransactionResults t = (TransactionResults)s.Deserialize(stream);
This, of course, moves the stream to the end, so then I cannot log the original value.
I tried seeking to the beginning to then read it with a StreamReader
, but it throws a NotSupportedException
As a test, I tried re-serializing it (I created all the objects again to avoid re using them to see if that was the problem)
private static string GetContents(TransactionResults t)
{
XmlSerializer s = new XmlSerializer(typeof(TransactionResults));
MemoryStream stream = new MemoryStream();
s.Serialize(stream, t);
return new StreamReader(stream).ReadToEnd();
}
This method returns an empty string.
(Obviously, if I invert the logic, the value gets logged, but then I cannot get the object)
What am I doing wrong? What would be the best way to deserialize the value into an object and log it as a string?