putting string into MemoryStream

  • Thread starter Thread starter Donovan
  • Start date Start date
D

Donovan

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.
 
Donovan said:
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.
 
Well, that assumes that the stream has a UTF-8 encoded string in 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.
 
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);
}
oStream.Seek(0,System.IO.SeekOrigin.Begin);
utrvContent.LoadFromXml(oStream);

Donovan said:
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.


contents
 
Donovan said:
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);
}
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.
 
Donovan said:
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.


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.
 
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 said:
Donovan said:
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);
}
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.
 
Donovan Zimmerman said:
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.
 
Back
Top