How to create a stream from a string in .Net

Yesterday I needed a way to take a string and create a stream from it. I did some searching but I didn't find any clean way. Here what I came up with in C#:

Stream s = new MemoryStream(ASCIIEncoding.Default.GetBytes("Test String"));

Does anyone know of any better or the recommended way to do this?

Why doesn't .Net have a string stream class?

Published Thursday, September 23, 2004 3:01 PM by puzzlehacker

Comments

# re: How to create a stream from a string in .Net

Do you mean something like the System.IO.StringReader class ?

Thursday, September 23, 2004 3:10 PM by Nidhogg

# re: How to create a stream from a string in .Net

Thanks Erv,

I didn't realize it until you mentioned it but ASCIIEncoding inherits from Encoding and the Default property actually calls GetACP underneath which will get the codepage from the system, which should work in most cases.

As far as what I was doing I needed to create a new stream to send as a parameter to a method (ReadXML on a DataSet object). It turns out that it also had an overload that took a TextReader so I just used that instead.

Thursday, September 23, 2004 4:14 PM by Wes

# re: How to create a stream from a string in .Net

May want to consider UTF8 encoding instead of ASCII (7-bit).

Wednesday, July 12, 2006 12:50 PM by Mark

# re: How to create a stream from a string in .Net

Very useful, you've just saved me an hour trying to figure out how to do this.

Wednesday, August 16, 2006 4:22 AM by Chris Green

# re: How to create a stream from a string in .Net

Thanks for posting, works great

Friday, September 1, 2006 11:44 AM by Duke

# re: How to create a stream from a string in .Net

Works Perfect...

Thankz a lot!..

Tuesday, December 19, 2006 9:11 AM by Raul

# re: How to create a stream from a string in .Net

If you have a string stringVal then use

StringReader strReader = new StringReader(stringVal);

StringReader is descendent of TextReader and in turn Stream class.

Wednesday, January 3, 2007 9:41 AM by Avinash

# re: How to create a stream from a string in .Net

If all you're trying to do is create a XmlTextReader and all you have is a string, this is the easiest way to do so:

XmlTextReader reader = new XmlTextReader(new StringReader(xmlString));

Friday, June 29, 2007 4:49 PM by NuBBeR

# re: How to create a stream from a string in .Net

no TextReader is not a Stream

Tuesday, March 4, 2008 3:15 PM by b

# re: How to create a stream from a string in .Net

Thanks - this was a great little bit of help and saved me hunting around the web for a few hours

Tuesday, May 20, 2008 1:19 PM by Paul

# re: How to create a stream from a string in .Net

Try using StringReader()

Wednesday, May 20, 2009 4:39 PM by Art

# re: How to create a stream from a string in .Net

public static class StringExtensions

{

public static MemoryStream ToMemoryStream(this string source) {

return source.ToMemoryStream(ASCIIEncoding.UTF8);

}

public static MemoryStream ToMemoryStream(

this string source, Encoding encoding)

{

return new MemoryStream(encoding.GetBytes(source));

}

}

Monday, July 27, 2009 3:52 PM by INTPnerd

# re: How to create a stream from a string in .Net

Stream s = new MemoryStream(ASCIIEncoding.Default.GetBytes("Test String"));

Thursday, October 15, 2009 1:01 PM by mr