string to a stream

P

prefersgolfing

I'm baffled! Some time back on a webcast I saw how to convert a string to a
stream but now I can't remember how. I pulling a string from a IBM Message
Queue which is an XML string, convert that to a stream, then send it to the
load method of an XMLDocument object. Thanks for your help.
 
B

Bryan Dickerson

You shouldn't need a stream if you have the xml in a string already:

Dim xTest As New Xml.XmlDocument
Dim file As New System.IO.StreamReader("C:\Test1.xml")
Dim sData As String = file.ReadToEnd()

xTest.LoadXml(sData)

This works for me.
 
A

Alister Neave

I have this function but note that it hasn't been fully tested.

Private Function StringToStream(ByVal strInString As String) As System.IO.Stream
'; Purpose: converts a string to a stream
'; Note: Not fully tested

Dim byt(strInString.Length - 1) As Byte
byt = System.Text.Encoding.UTF8.GetBytes(strInString)
Dim stm As System.IO.Stream = New System.IO.MemoryStream(strInString.Length)
stm.Write(byt, 0, byt.Length)
stm.Position = 0

Return stm

End Function
 
P

prefersgolfing

The idea here is I don't want to use a file. I don't need to use a file. I
have a string and want to convert that to a stream.
 
P

prefersgolfing

Bryan,

My apologies. I didn't read your comment under the header and didn't notice
that you called the .LoadXML method. This works perfectly. Thanks!
 
B

Bryan Dickerson

You're welcome! I can count you as my first successful assistance on this
newsgroup! (Can I have my MVP Title now??!! Just kidding!)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top