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

Nighogg,
Thanks that will actually work for what I'm doing, because there is a overload that takes a TextReader. I don't know how I overlooked that before, I guess I was too focused on getting a stream.

However StringReader doesn't produce a stream, at least I don't see any easy method to create a stream from it. So I'm still curious to see if there is a better method of creating a stream from a string.

Thursday, September 23, 2004 3:38 PM by Wes

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

There is no other conceptual way. A stream is a series of bytes. You decide what encoding you want to use (usually not ASCII, but UTF-8 or windows-1252) and you convert it to a series of bytes. Your code is about the most concise and efficient way to do that.

If you just need to get your string *into* an existing stream (a file stream, etc), you may choose to use a StreamWriter instead (which still needs you to tell it what encoding to use).

Thursday, September 23, 2004 3:52 PM by Erv Walter

# 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

# Comment créer un Stream avec une String

Lien : Wes' Puzzling Blog Très utile ! Stream s = new MemoryStream(ASCIIEncoding.Default.GetBytes("Test String"))...

Tuesday, July 11, 2006 6:55 AM by Sébastien Lachance

# 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 01, 2006 11:44 AM by Duke

# String Streams in .Net

I while ago (almost 2 years) I posted a way to create a string stream in .Net. There has been a number...

Monday, September 04, 2006 8:01 PM by Wes' Blog

# String Streams in .Net

I while ago (almost 2 years) I posted a way to create a string stream in .Net. There has been a number

Monday, September 04, 2006 8:02 PM by Wes' Puzzling Blog

# 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 03, 2007 9:41 AM by Avinash

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

Yes that is true you can check out my updated post http://weblogs.asp.net/whaggard/archive/2006/09/04/String-Streams-in-.Net.aspx for more information on this.

Wednesday, January 03, 2007 2:48 PM by puzzlehacker

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

Avinash: TextReader derives from MarshalByRefObject, not Stream.

See msdn2.microsoft.com/.../system.io.textreader.aspx

Thursday, May 24, 2007 10:21 AM by puddinhead wilson

# C# - Creating a stream from a string - GadgetNate

Pingback from  C# - Creating a stream from a string - GadgetNate

Tuesday, June 12, 2007 6:06 PM by C# - Creating a stream from a string - GadgetNate

# 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 04, 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

Never, ever use ASCIIEncoding.Default!!! But thanks for your post, thats how I got aware ;-)

startbigthinksmall.wordpress.com/.../utf8encodingdefault-encodingutf8-net-c

Tuesday, January 20, 2009 1:31 PM by Lars Corneliussen

# String To Stream « A Wicked Blog

Pingback from  String To Stream « A Wicked Blog

Friday, March 13, 2009 4:23 AM by String To Stream « A Wicked Blog

# 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

# Stream string | Grimeycoalitio

Pingback from  Stream string | Grimeycoalitio

Wednesday, September 21, 2011 8:08 AM by Stream string | Grimeycoalitio

# Create stream | Racetracksuppl

Pingback from  Create stream | Racetracksuppl

Thursday, January 31, 2013 12:10 PM by Create stream | Racetracksuppl

Leave a Comment

(required) 
(required) 
(optional)
(required)