String to StreamReader

  • Thread starter Thread starter Joe Hanna
  • Start date Start date
J

Joe Hanna

Hello,

Does anyone know how to convert a String variable into a System.IO.StreamReader object ?

Thanks in advance,
Joe
 
StreamReader operates on files. I suppose you are looking for StringReader class.
Hello,

Does anyone know how to convert a String variable into a System.IO.StreamReader object ?

Thanks in advance,
Joe
 
Thanks for your reply Alex.

I have a Method processing XML. The XML originally came from a file but now I can also get the XML back from another Method as a String. I didn't necessarily want to create another Overloaded Method to handle the String but I guess I have no choice.

Joe

StreamReader operates on files. I suppose you are looking for StringReader class.
Hello,

Does anyone know how to convert a String variable into a System.IO.StreamReader object ?

Thanks in advance,
Joe
 
Maybe something like this will work for you.

Imports System.IO
Imports System.Text
....
Public Function GetSR(ByVal Input As String) As StreamReader
GetSR = New StreamReader(New MemoryStream(New
ASCIIEncoding().GetBytes(Input)))
End Function
 
| Imports System.IO
| Imports System.Text
| ...
| Public Function GetSR(ByVal Input As String) As StreamReader
| GetSR = New StreamReader(New MemoryStream(New
| ASCIIEncoding().GetBytes(Input)))
| End Function

Please note that this solution won't work if you string contains any
non-ASCII characters.

- Roman
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
| NNTP-Posting-Date: Sat, 17 Jan 2004 10:25:59 -0600
| From: "Gordon Freeman" <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.compactframework
| References: <#[email protected]>
<[email protected]>
<[email protected]>
| Subject: Re: String to StreamReader
| Date: Sat, 17 Jan 2004 11:25:50 -0500
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <[email protected]>
| Lines: 11
| NNTP-Posting-Host: 68.54.189.138
| X-Trace:
sv3-2eRWRlVSHbw3nu7z9EPfFE6SqZQfrSjXzeeJXIG5iT6aZmNQzxnwSHMGM7PNHecwrUr0h7NQ
h8yBoum!MTQQqMN7NcEV0MjZVlRbhLBB9hP5phCT+/dJFzPSuRPT8STHz4XNZwnsE7xIeA==
| X-Complaints-To: (e-mail address removed)
| X-DMCA-Complaints-To: (e-mail address removed)
| X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
| X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your
complaint properly
| X-Postfilter: 1.1
| Path:
cpmsftngxa07.phx.gbl!cpmsftngxa10.phx.gbl!TK2MSFTNGXA05.phx.gbl!TK2MSFTNGP08
..phx.gbl!newsfeed00.sul.t-online.de!t-online.de!newsfeed.icl.net!newsfeed.fj
serv.net!newshosting.com!nx02.iad01.newshosting.com!216.196.98.140.MISMATCH!
border1.nntp.ash.giganews.com!border2.nntp.sjc.giganews.com!border1.nntp.sjc
..giganews.com!nntp.giganews.com!local1.nntp.sjc.giganews.com!nntp.comcast.co
m!news.comcast.com.POSTED!not-for-mail
| Xref: cpmsftngxa07.phx.gbl
microsoft.public.dotnet.framework.compactframework:43312
| X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework
|
| Maybe something like this will work for you.
|
| Imports System.IO
| Imports System.Text
| ...
| Public Function GetSR(ByVal Input As String) As StreamReader
| GetSR = New StreamReader(New MemoryStream(New
| ASCIIEncoding().GetBytes(Input)))
| End Function
|
|
|

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Thanks for pointing out the problem. So what is the solution Roman? Would
using UTF8Encoding be better than ASCIIEncoding?
 
Thanks Gordon and Roman.

Roman Batoukov said:
| Imports System.IO
| Imports System.Text
| ...
| Public Function GetSR(ByVal Input As String) As StreamReader
| GetSR = New StreamReader(New MemoryStream(New
| ASCIIEncoding().GetBytes(Input)))
| End Function

Please note that this solution won't work if you string contains any
non-ASCII characters.

- Roman
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
| NNTP-Posting-Date: Sat, 17 Jan 2004 10:25:59 -0600
| From: "Gordon Freeman" <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.compactframework
| References: <#[email protected]>
<[email protected]>
<[email protected]>
| Subject: Re: String to StreamReader
| Date: Sat, 17 Jan 2004 11:25:50 -0500
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <[email protected]>
| Lines: 11
| NNTP-Posting-Host: 68.54.189.138
| X-Trace:
sv3-2eRWRlVSHbw3nu7z9EPfFE6SqZQfrSjXzeeJXIG5iT6aZmNQzxnwSHMGM7PNHecwrUr0h7NQ
h8yBoum!MTQQqMN7NcEV0MjZVlRbhLBB9hP5phCT+/dJFzPSuRPT8STHz4XNZwnsE7xIeA==
| X-Complaints-To: (e-mail address removed)
| X-DMCA-Complaints-To: (e-mail address removed)
| X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
| X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your
complaint properly
| X-Postfilter: 1.1
| Path:
cpmsftngxa07.phx.gbl!cpmsftngxa10.phx.gbl!TK2MSFTNGXA05.phx.gbl!TK2MSFTNGP08phx.gbl!newsfeed00.sul.t-online.de!t-online.de!newsfeed.icl.net!newsfeed.fjserv.net!newshosting.com!nx02.iad01.newshosting.com!216.196.98.140.MISMATCH!border1.nntp.ash.giganews.com!border2.nntp.sjc.giganews.com!border1.nntp.sjcgiganews.com!nntp.giganews.com!local1.nntp.sjc.giganews.com!nntp.comcast.co
m!news.comcast.com.POSTED!not-for-mail
| Xref: cpmsftngxa07.phx.gbl
microsoft.public.dotnet.framework.compactframework:43312
| X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework
|
| Maybe something like this will work for you.
|
| Imports System.IO
| Imports System.Text
| ...
| Public Function GetSR(ByVal Input As String) As StreamReader
| GetSR = New StreamReader(New MemoryStream(New
| ASCIIEncoding().GetBytes(Input)))
| End Function
|
|
|

This posting is provided "AS IS" with no warranties, and confers no rights.
 
The choice of encoding depends on what you want to do with resutling
sequence of bytes.
You can't tell that one encoding is "better" than other. If your original
string is known to contain only ASCII character (\u0000-\u007F), your
solution is fine. oll other characters will be replaced with '?'. But if
you want to encode arbitrary international strings without loss of
information, UnicodeEncoding or UTF8 encoding should be used. Please keep
in mind that every unicode character in the original string can be
represented by more than one byte in a resulting byte stream. In case of
Unicode encoding it's 2 bytes per unicode character (endianess is
important), in case of UTF8 it's variable length (1-4 bytes per Unicode
character).

- Roman

This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
| NNTP-Posting-Date: Tue, 20 Jan 2004 19:20:57 -0600
| From: "Gordon Freeman" <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.compactframework
| References: <#[email protected]>
<[email protected]>
<[email protected]> <[email protected]>
<[email protected]>
| Subject: Re: String to StreamReader
| Date: Tue, 20 Jan 2004 20:20:48 -0500
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <[email protected]>
| Lines: 19
| NNTP-Posting-Host: 68.54.189.138
| X-Trace:
sv3-dmQSN4qGf6M6HgyTNQWm+/DUxgKquI42sCo8pU78uKrdreKbtmx9+odyDUkNUk/jliaTV8Lv
o4s4LsD!kQ6tNHSfG5RVfdNxaKY/1K5AvrSnWcqEqhAThBX0Md9aA0JRcHRKOne75O8t0A==
| X-Complaints-To: (e-mail address removed)
| X-DMCA-Complaints-To: (e-mail address removed)
| X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
| X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your
complaint properly
| X-Postfilter: 1.1
| Path:
cpmsftngxa07.phx.gbl!cpmsftngxa06.phx.gbl!TK2MSFTNGXS01.phx.gbl!TK2MSFTNGXA0
5.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.sul.t-online.de!t-online.de!newsfe
ed.icl.net!newsfeed.fjserv.net!news.maxwell.syr.edu!newshosting.com!nx02.iad
01.newshosting.com!216.196.98.140.MISMATCH!border1.nntp.ash.giganews.com!bor
der2.nntp.sjc.giganews.com!border1.nntp.sjc.giganews.com!nntp.giganews.com!l
ocal1.nntp.sjc.giganews.com!nntp.comcast.com!news.comcast.com.POSTED!not-for
-mail
| Xref: cpmsftngxa07.phx.gbl
microsoft.public.dotnet.framework.compactframework:43581
| X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework
|
| Thanks for pointing out the problem. So what is the solution Roman? Would
| using UTF8Encoding be better than ASCIIEncoding?
|
| | > | Imports System.IO
| > | Imports System.Text
| > | ...
| > | Public Function GetSR(ByVal Input As String) As StreamReader
| > | GetSR = New StreamReader(New MemoryStream(New
| > | ASCIIEncoding().GetBytes(Input)))
| > | End Function
| >
| > Please note that this solution won't work if you string contains any
| > non-ASCII characters.
| >
| > - Roman
|
|
|

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top