Solved

Load stream with text

Posted on 2004-10-12
15
241 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Saveas need to save a copy 16 26
Anyway to make "All" the default in the dropdown? 6 34
Not showing JavaScript in the list 5 36
Vb. Net application freezes 9 28
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

930 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

9 Experts available now in Live!

Get 1:1 Help Now