473,511 Members | 14,052 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Converting a MemoryStream of bytes to a string

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.
May 23 '06 #1
3 17998
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" <srhartone[no spam]@yahoo.com> wrote in message
news:%2***************@TK2MSFTNGP02.phx.gbl...
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.

May 23 '06 #2
Ollie,

Thank you, simplified and works a treat.

Cheers
Simon.

"Ollie Riches" <ol**********@phoneanalyser.net> wrote in message
news:u6**************@TK2MSFTNGP02.phx.gbl...
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" <srhartone[no spam]@yahoo.com> wrote in message
news:%2***************@TK2MSFTNGP02.phx.gbl...
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.


May 23 '06 #3
<"Simon Hart" <srhartone[no spam]@yahoo.com>> wrote:

<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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 24 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2328
by: Pete Davis | last post by:
I've never done this in C# so I don't know what the appropriate way of doing it is. I've got an array of bytes and I need to convert the array into "usable" data. For example, the first 4 bytes...
4
9191
by: Kai Bohli | last post by:
Hi all ! This message has been posted on .net.general, but I reposts it here due to lack of replies. This is my first day with Visual Studio and C#, and I'm trying to send "raw data" to the...
8
4533
by: iyuen | last post by:
I'm having problems with converting a byte array to an image object~ My byte array is an picture in VB6 StdPicture format. I've used propertybag to convert the picture into base64Array format in...
12
7708
by: Mark Rae | last post by:
Hi, Can anyone please tell me how to convert an unserializeable object say, a System.Web.Mail.MailMessage object, to a byte array and then convert the byte array to a Base64 string? Any...
13
2832
by: Don | last post by:
When I run the following code, the MemoryStream's Position is always set to 762 instead of 0, which is what I would expect: Dim bmp As Image Dim ms As MemoryStream bmp = New...
5
26221
by: Naamat | last post by:
Hello, I am the sample FPSEPublish (http://blog.baeke.info/blog/_archives/2005/3/3/393158.html) code to upload a document to Sharepoint (WSS). This works perfectly for samll documents. ...
10
17812
by: Asaf | last post by:
Hi, I am trying to Compress & Decompress a DataSet. When running this code I am receiving the error: System.Xml.XmlException was unhandled Message="Root element is missing."...
3
6627
by: grawsha2000 | last post by:
Hi, I'm trying to convert this simple string into image: Dim bytes() as byte()=System.text.Encoding.ascii.GetBytes("123") Dim memStream as System.IO.MemoryStream Dim img as image ...
2
10318
by: Laurent Navarro | last post by:
Hello, I am using a library which returns a byte containing RAW data, ie all pixels' color values coded in a byte array without header. I would like to save those data into a JPEG file so I...
0
7144
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7356
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
5671
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5069
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4741
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3214
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1577
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
785
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
449
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.