views:

61

answers:

2

I have a SOAP object that I want to capture as a string. This is what I have now:

RateRequest request = new RateRequest();
//Do some stuff to request here

SoapFormatter soapFormat = new SoapFormatter();
using (MemoryStream myStream = new MemoryStream())
{
  soapFormat.Serialize(myStream, request);
  myStream.Position = 0;
  using (StreamReader sr = new StreamReader(myStream))
  {
    string reqString = sr.ReadToEnd();
  }
}

Is there a more elegant way to do this? I don't care that much about the resulting string format - just so it's human readable. XML is fine.

A: 

No, that's pretty much the way to do it. You could always factor this out to a method which will do this work for you, and then you can just reduce it to a single call where you need it.

casperOne
A: 

I think you can also do this:

soapFormat.Serialize(myStream, request); string xml=System.Text.ASCIIEncoding.ASCII.GetString(myStream.GetBuffer());

jm