By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,195 Members | 1,660 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,195 IT Pros & Developers. It's quick & easy.

putting string into MemoryStream

P: n/a
I can't believe this is causing me as much difficulty as it is, but I have
an Infragistics UltraTreeview control that I want to persist whatever the
user has in the tree. It has a method SaveAsXml which puts the contents into
a MemoryStream, which I convert to a string for persistance (I know
Infragistics allows me to, but I can't store as a file for various reasons,
so it's not a valid alternative). I'm using this code to put into a string

System.IO.MemoryStream oStream = new MemoryStream();
System.IO.StreamReader oReader = null;
String sXML="";

utrvContent.SaveAsXml(oStream); //saves the contents to a stream
oStream.Seek(0,System.IO.SeekOrigin.Begin);
oReader= new StreamReader((System.IO.Stream)oStream);
sXML= oReader.ReadToEnd();
So now I get this string and I need to load it into a stream to load back
into the tree control like this
utrvContent.LoadFromXml(oStream);

I can not figure out how to load the string I persisted into a MemoryStream.
Thanks in advance for any help.
Nov 16 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Donovan <do*****@ymedocid.net> wrote:
I can't believe this is causing me as much difficulty as it is, but I have
an Infragistics UltraTreeview control that I want to persist whatever the
user has in the tree. It has a method SaveAsXml which puts the contents into
a MemoryStream, which I convert to a string for persistance (I know
Infragistics allows me to, but I can't store as a file for various reasons,
so it's not a valid alternative). I'm using this code to put into a string

System.IO.MemoryStream oStream = new MemoryStream();
System.IO.StreamReader oReader = null;
String sXML="";

utrvContent.SaveAsXml(oStream); //saves the contents to a stream
oStream.Seek(0,System.IO.SeekOrigin.Begin);
oReader= new StreamReader((System.IO.Stream)oStream);
sXML= oReader.ReadToEnd();
Well, that assumes that the stream has a UTF-8 encoded string in it.
Does it?
So now I get this string and I need to load it into a stream to load back
into the tree control like this

utrvContent.LoadFromXml(oStream);

I can not figure out how to load the string I persisted into a
MemoryStream. Thanks in advance for any help.


Well, you can use StreamWriter, and avoid closing it, then rewind the
stream. Or use Encoding.GetBytes and construct a MemoryStream from the
result.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2

P: n/a
> Well, that assumes that the stream has a UTF-8 encoded string in it.
Does it?
I'm not sure yet what it has in it yet. I was going to work with that when I
figured out if I can do this. I was setting it to Unicode, but took out
while trying to get this work. I'll have to check that.
Well, you can use StreamWriter, and avoid closing it, then rewind the
stream. Or use Encoding.GetBytes and construct a MemoryStream from the
result.
But a StreamWriter takes a path, not a string. It seemed to me that a
StringReader was the only thing that could read a string directly, but I
can't figure out how to put that into a stream. I'll look into the
Encoding.GetBytes. Thanks.
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om... Donovan <do*****@ymedocid.net> wrote:
I can't believe this is causing me as much difficulty as it is, but I have an Infragistics UltraTreeview control that I want to persist whatever the user has in the tree. It has a method SaveAsXml which puts the contents into a MemoryStream, which I convert to a string for persistance (I know
Infragistics allows me to, but I can't store as a file for various reasons, so it's not a valid alternative). I'm using this code to put into a string
System.IO.MemoryStream oStream = new MemoryStream();
System.IO.StreamReader oReader = null;
String sXML="";

utrvContent.SaveAsXml(oStream); //saves the contents to a stream
oStream.Seek(0,System.IO.SeekOrigin.Begin);
oReader= new StreamReader((System.IO.Stream)oStream);
sXML= oReader.ReadToEnd();


Well, that assumes that the stream has a UTF-8 encoded string in it.
Does it?
So now I get this string and I need to load it into a stream to load back into the tree control like this

utrvContent.LoadFromXml(oStream);

I can not figure out how to load the string I persisted into a
MemoryStream. Thanks in advance for any help.


Well, you can use StreamWriter, and avoid closing it, then rewind the
stream. Or use Encoding.GetBytes and construct a MemoryStream from the
result.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #3

P: n/a
The encoding worked! Thanks.

Changed my save routine to
oReader= new
StreamReader((System.IO.Stream)oStream,System.Text .ASCIIEncoding.ASCII);

and then my read routine is

System.Text.Encoding aCode = System.Text.Encoding.ASCII;
byte[] acodeBytes = aCode.GetBytes(sXML);
for (int i=0;i<acodeBytes.Length-1;i++)
{
oStream.WriteByte(acodeBytes[i]);
}
oStream.Seek(0,System.IO.SeekOrigin.Begin);
utrvContent.LoadFromXml(oStream);

"Donovan" <do*****@ymedocid.net> wrote in message
news:OO**************@TK2MSFTNGP12.phx.gbl...
Well, that assumes that the stream has a UTF-8 encoded string in it.
Does it?
I'm not sure yet what it has in it yet. I was going to work with that when

I figured out if I can do this. I was setting it to Unicode, but took out
while trying to get this work. I'll have to check that.
Well, you can use StreamWriter, and avoid closing it, then rewind the
stream. Or use Encoding.GetBytes and construct a MemoryStream from the
result.
But a StreamWriter takes a path, not a string. It seemed to me that a
StringReader was the only thing that could read a string directly, but I
can't figure out how to put that into a stream. I'll look into the
Encoding.GetBytes. Thanks.
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Donovan <do*****@ymedocid.net> wrote:
I can't believe this is causing me as much difficulty as it is, but I have an Infragistics UltraTreeview control that I want to persist whatever the user has in the tree. It has a method SaveAsXml which puts the
contents into a MemoryStream, which I convert to a string for persistance (I know
Infragistics allows me to, but I can't store as a file for various reasons, so it's not a valid alternative). I'm using this code to put into a string
System.IO.MemoryStream oStream = new MemoryStream();
System.IO.StreamReader oReader = null;
String sXML="";

utrvContent.SaveAsXml(oStream); //saves the contents to a stream
oStream.Seek(0,System.IO.SeekOrigin.Begin);
oReader= new StreamReader((System.IO.Stream)oStream);
sXML= oReader.ReadToEnd();


Well, that assumes that the stream has a UTF-8 encoded string in it.
Does it?
So now I get this string and I need to load it into a stream to load back into the tree control like this

utrvContent.LoadFromXml(oStream);

I can not figure out how to load the string I persisted into a
MemoryStream. Thanks in advance for any help.


Well, you can use StreamWriter, and avoid closing it, then rewind the
stream. Or use Encoding.GetBytes and construct a MemoryStream from the
result.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Nov 16 '05 #4

P: n/a
Donovan <do*****@ymedocid.net> wrote:
Well, that assumes that the stream has a UTF-8 encoded string in it.
Does it?


I'm not sure yet what it has in it yet. I was going to work with that when I
figured out if I can do this. I was setting it to Unicode, but took out
while trying to get this work. I'll have to check that.


Right.
Well, you can use StreamWriter, and avoid closing it, then rewind the
stream. Or use Encoding.GetBytes and construct a MemoryStream from the
result.


But a StreamWriter takes a path, not a string.


The constructor *can* take a path - or it can take a stream. You can
then write to the StreamWriter, and the data gets written to the
stream.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5

P: n/a
Donovan <do*****@ymedocid.net> wrote:
The encoding worked! Thanks.

Changed my save routine to
oReader= new
StreamReader((System.IO.Stream)oStream,System.Text .ASCIIEncoding.ASCII);

and then my read routine is

System.Text.Encoding aCode = System.Text.Encoding.ASCII;
byte[] acodeBytes = aCode.GetBytes(sXML);
for (int i=0;i<acodeBytes.Length-1;i++)
{
oStream.WriteByte(acodeBytes[i]);
}
oStream.Seek(0,System.IO.SeekOrigin.Begin);
utrvContent.LoadFromXml(oStream);


Are you *sure* that it's being saved as ASCII? If it's not, you'll be
losing data.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6

P: n/a
I'm guessing I would know, because this is loading data into the tree
control. If it loses data, I'm assuming it won't be a properly constructed
xml document and won't load. I'll keep on eye on it though. Thanks a lot.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Donovan <do*****@ymedocid.net> wrote:
The encoding worked! Thanks.

Changed my save routine to
oReader= new
StreamReader((System.IO.Stream)oStream,System.Text .ASCIIEncoding.ASCII);

and then my read routine is

System.Text.Encoding aCode = System.Text.Encoding.ASCII;
byte[] acodeBytes = aCode.GetBytes(sXML);
for (int i=0;i<acodeBytes.Length-1;i++)
{
oStream.WriteByte(acodeBytes[i]);
}
oStream.Seek(0,System.IO.SeekOrigin.Begin);
utrvContent.LoadFromXml(oStream);


Are you *sure* that it's being saved as ASCII? If it's not, you'll be
losing data.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #7

P: n/a
Donovan Zimmerman <do*****@ymedocid.net> wrote:
I'm guessing I would know, because this is loading data into the tree
control. If it loses data, I'm assuming it won't be a properly constructed
xml document and won't load. I'll keep on eye on it though. Thanks a lot.


I would try putting some Unicode non-ASCII characters into your data -
put some accented characters etc in and see what happens.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.