Converting a MemoryStream of bytes to a string

S

Simon Hart

Sounds an easy task and should be, but the code result is a blank byte
array.

Comments within code.

The code is:

Stream sr = new MemoryStream();
XmlSerializer s = new XmlSerializer(typeof(CRM.BusinessEntity));
s.Serialize(sr, response.BusinessEntity);

//MemoryStream does contain correct byte array after the above.

byte[] serBuf = new byte[(int)sr.Length - 1];
sr.Read(serBuf, 0, serBuf.Length - 1);
sr.Close(); //Close the stream to release the memory.

//At this point serBuf is never set with the byte array held in the
MemoryStream!

//Now we want to convert the array of bytes to a string.
//We do this by using the Encoding class.
string myData = System.Text.Encoding.ASCII.GetString(serBuf);


Any help would be great.
Simon.
 
O

Ollie Riches

If you want to serialize to string try this:

StringBuilder sb = new StringBuilder();
XmlSerializer ser = new XmlSerializer(typeof(CRM.BusinessEntity));
using(StringWriter sw = new StringWriter(sb))
{
ser.Serialize(sw, job);
return sb.ToString();
}

Ollie Riches
 
S

Simon Hart

Ollie,

Thank you, simplified and works a treat.

Cheers
Simon.

Ollie Riches said:
If you want to serialize to string try this:

StringBuilder sb = new StringBuilder();
XmlSerializer ser = new XmlSerializer(typeof(CRM.BusinessEntity));
using(StringWriter sw = new StringWriter(sb))
{
ser.Serialize(sw, job);
return sb.ToString();
}

Ollie Riches

Simon Hart said:
Sounds an easy task and should be, but the code result is a blank byte
array.

Comments within code.

The code is:

Stream sr = new MemoryStream();
XmlSerializer s = new XmlSerializer(typeof(CRM.BusinessEntity));
s.Serialize(sr, response.BusinessEntity);

//MemoryStream does contain correct byte array after the above.

byte[] serBuf = new byte[(int)sr.Length - 1];
sr.Read(serBuf, 0, serBuf.Length - 1);
sr.Close(); //Close the stream to release the memory.

//At this point serBuf is never set with the byte array held in the
MemoryStream!

//Now we want to convert the array of bytes to a string.
//We do this by using the Encoding class.
string myData = System.Text.Encoding.ASCII.GetString(serBuf);


Any help would be great.
Simon.
 
J

Jon Skeet [C# MVP]

<snip>

Leaving aside the rest of your question, this bit is very, very dodgy:
//Now we want to convert the array of bytes to a string.
//We do this by using the Encoding class.
string myData = System.Text.Encoding.ASCII.GetString(serBuf);

If there are any non-ASCII characters in the XML, you will lose data.
You should use the same encoding that the XML document itself uses.
This is likely to be UTF-8, but you should check.

Note that in the general case, you should convert arbitrary binary data
into strings using something like base64. In this case it's okay
because the binary data is actually encoded text data.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top