I have string object. I need to pass this data to another object of type XYZ. But this object of type XYZ is taking only System.IO.Stream. So how to convert the string data into a stream so that object of XYZ type can use this string data?
+10
A:
You'll have to pick a text encoding to use to translate the string into a byte array, then use a MemoryStream
to call your function. For example:
using(System.IO.MemoryStream ms = new System.IO.MemoryStream(
System.Text.Encoding.UTF16.GetBytes(yourString))
{
XYZ(ms);
}
You can alter UTF16
to be whatever encoding you'd like to use to pass the string.
Adam Robinson
2010-09-28 12:53:01
It is working, thanks.
mohang
2010-09-28 12:57:41
A:
Assuming you want the string's stream encoded in UTF8:
System.IO.MemoryStream mStream = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes( "the string"));
Depending on what you really want to do, you might be better served using the StringReader class. It's not an IO.Stream, but it makes for easy text-oriented reading of a string.
Aamod Thakur
2010-09-28 12:56:54
A:
This code loads formatted text (rtf) into RichTextBox
TextRange tr = new TextRange(RichTextBox1.Document.ContentStart,RichTextBox1.Document.ContentEnd);
string s = myStringData; //myStringData is a string in some format - rtf, xml, etc..
MemoryStream ms = new MemoryStream(s);
tr.Load(ms, DataFormats.Rtf);
Alex
2010-09-28 13:00:16