Solved

Load stream with text

Posted on 2004-10-12
15
246 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
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
 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

738 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