Solved

Load stream with text

Posted on 2004-10-12
15
240 Views
Last Modified: 2013-11-18
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
Comment
Question by:Metallicraft
  • 7
  • 7
15 Comments
 
LVL 28

Expert Comment

by:iboutchkine
ID: 12288826
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
 
LVL 14

Expert Comment

by:AvonWyss
ID: 12290840
You can get the data from any .NET stream. This can be memory, disk, network, or anything else you implement as stream...
0
 

Author Comment

by:Metallicraft
ID: 12294126
Where do I put my text string in the example given?
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 12295152
Do you mean something like this:

StrintReader sr=new StringReader("<root>some xml data</root>");
XmlTextReader xr=new XmlTextReader(sr);
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 12295153
Oops, typo. StringReader is what I meant.
0
 

Author Comment

by:Metallicraft
ID: 12297082
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
 
LVL 14

Expert Comment

by:AvonWyss
ID: 12297149
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:Metallicraft
ID: 12297200
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
 

Author Comment

by:Metallicraft
ID: 12297214
Please, disregard the "Dim xmlBuffer As Byte() = System.Text.Encoding.ASCII.GetBytes(s)" line, it's old code.
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 12297290
Why are you suddently using an XmlTextWriter? What are you trying to do?
0
 

Author Comment

by:Metallicraft
ID: 12299226
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
 
LVL 14

Expert Comment

by:AvonWyss
ID: 12299657
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
 

Author Comment

by:Metallicraft
ID: 12300696
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
 
LVL 14

Accepted Solution

by:
AvonWyss earned 125 total points
ID: 12301141
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
 

Author Comment

by:Metallicraft
ID: 12301733
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

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now