Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 256
  • Last Modified:

Load stream with text

I have an XMLTextReader that I want to load with a stream.  Can I load the stream with the text that I want without having to get the text from a file?
0
Metallicraft
Asked:
Metallicraft
  • 7
  • 7
1 Solution
 
iboutchkineCommented:
Dim ms As MemoryStream
Dim wrt As XmlTextWriter

ms = New MemoryStream()
wrt = New XmlTextWriter(ms, Nothing)

'...xml processing
wrt.WriteEndElement()
wrt.Flush()
ms.Position = 0


wrt.Flush()

Dim fs As New FileStream("C:\myfiles\f1.xml", FileMode.CreateNew) '
ms.WriteTo(fs)
ms.Flush()
ms.Close()
0
 
AvonWyssCommented:
You can get the data from any .NET stream. This can be memory, disk, network, or anything else you implement as stream...
0
 
MetallicraftAuthor Commented:
Where do I put my text string in the example given?
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
AvonWyssCommented:
Do you mean something like this:

StrintReader sr=new StringReader("<root>some xml data</root>");
XmlTextReader xr=new XmlTextReader(sr);
0
 
AvonWyssCommented:
Oops, typo. StringReader is what I meant.
0
 
MetallicraftAuthor Commented:
In the example, which looks like exactly what I want, the memorsystream (ms) is loaded into the xmltextwriter (wrt = New XmlTextWriter(ms, Nothing)).  How do I get my string value ("<root>some xml data</root>") into the memorystream?
0
 
AvonWyssCommented:
Why do you need it in the memorystream? Note that a stream is byte-based, whereas a string is char-based. Now chars and bytes used to be the same thing, but in .NET, they aren't, since chars are unicode and therefore more than 8 bits (they are 16 bits). Therefore, whenever you make a transition from a stream to a string or the other way around, you need to apply an encoding. The default encoding for XML data is UTF8, but you can use other encodings.

If you want to just load a string into a memory stream, do it like this:

byte[] encodedString=System.text.Encoding.UTF8.GetBytes("<root>some xml data</root>");
ms.Write(encodedString, 0, encodedString.Length);

But I don't think that this is what you want. Look again at my previous comment; the use of the StringReader allows you to specify any text you want and load it using your XmlTextReader.
0
 
MetallicraftAuthor Commented:
Here is what I figured out on the loading of the memory stream:

        Dim ms As MemoryStream
        Dim wrt As XmlTextWriter

        Dim s As String = "<?xml version='1.0' standalone='yes'?><Advertisements>  <Ad>    <ImageUrl>../images/Ads/Ad1.gif</ImageUrl>  </Ad>  <Ad>    <ImageUrl>../images/Ads/Ad2.gif</ImageUrl>  </Ad>  <Ad>    <ImageUrl>../images/Ads/Ad3.gif</ImageUrl>  </Ad>  <Ad>    <ImageUrl>../images/Ads/Ad4.gif</ImageUrl>  </Ad>  <Ad>    <ImageUrl>../images/Ads/Ad5.gif</ImageUrl>  </Ad>  <Ad>    <ImageUrl>../images/Ads/Ad6.gif</ImageUrl>  </Ad></Advertisements>"
        Dim xmlBuffer As Byte() = System.Text.Encoding.ASCII.GetBytes(s)
        Dim bytes As Byte() = System.Text.Encoding.ASCII.GetBytes(s)

        ms = New MemoryStream(bytes)
        wrt = New XmlTextWriter(ms, Nothing)

        wrt.WriteEndElement()
        wrt.Flush()
        ms.Position = 0
        wrt.Flush()

        Dim fs As New FileStream("C:\f1.xml", FileMode.CreateNew)
        ms.WriteTo(fs)
        ms.Flush()
        ms.Close()

....but now I'm getting an error on the line that reads " wrt.WriteEndElement()".  The error is:

An unhandled exception of type 'System.InvalidOperationException' occurred in system.xml.dll
Additional information: There was no XML start tag open.
0
 
MetallicraftAuthor Commented:
Please, disregard the "Dim xmlBuffer As Byte() = System.Text.Encoding.ASCII.GetBytes(s)" line, it's old code.
0
 
AvonWyssCommented:
Why are you suddently using an XmlTextWriter? What are you trying to do?
0
 
MetallicraftAuthor Commented:
Hmmm, I'm tripping over my own code... o.0

XmlTextReader is what I need, I forgot my question when I saw the example given to me.  My apologies for the confusion.

Here's what I'm doing.  I have a zip file that contains, among other things, an xml file.  Without unzipping any files to the hard drive, I want to locate the xml within, then stream its contents and an xml schema file's contents into another process that will validate.  Opening and reading and streaming from the zip, I think I have figured out.  But, was getting hung up getting said stream into an XMLText Reader.
0
 
AvonWyssCommented:
Ok. So you have a Stream from the ZIP file and you want to feed it to the XmlTextReader? Just pass it in the constructor, it supports this directly:

XmlTextReader xtr=new XmlTextReader(yourStream);
0
 
MetallicraftAuthor Commented:
Well, I can get the string (instead of a memorystream) using:

New ASCIIEncoding().GetString(data, 0, size)

I know this is returning my xml file's contents because I can put it in a textbox and view it.

However, when I do the following:

Dim sr As New StringReader(New ASCIIEncoding().GetString(data, 0, size))
Dim xr As XmlTextReader = New XmlTextReader(sr)

I can't figure out how to check either one (StringReader or XmlTextReader) to make sure all of the data is there.  The ultimate goal was to get it into an XmlTextReader, which it appears we have done, but I'd like to test xr to make sure.
0
 
AvonWyssCommented:
You can test this by loading it into an XmlDocument:

XmlDocument doc=new XmlDocument();
doc.Load(xr);
textBox1.Text=doc.OuterXml;

Note that you should NOT use an ASCII encoding, or all special chars will get trashed (everything like ©®±¶¼½¾öäüÄÖÜàéèÀÉÈñÑ). Use the UTF8 encoding as I showed it: Encoding.UTF8 - no need to always new an new instance of this object!
0
 
MetallicraftAuthor Commented:
I changed the encoding to utf8, and moved the new out of the loop.  Thanks for all your help and patience, I think I'm set.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now