Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB.NET 1.0 > Pro VB.NET 2002/2003
|
Pro VB.NET 2002/2003 For advanced Visual Basic coders working .NET version 2002/2003. Beginning-level questions will be redirected to other forums, including Beginning VB.NET.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro VB.NET 2002/2003 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 4th, 2004, 09:03 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 231
Thanks: 0
Thanked 0 Times in 0 Posts
Default Writing more than 1Kb to a MemoryStream

I have written a routine that accepts as a parameter a System.IO.TextWriter, this is then used to create a System.Xml.XmlTextWriter and I proceed to write some Xml content to the stream using the XmlTextWriter. When I pass in a System.IO.StringWriter this works well and I can display the Xml content.

I now want to encrypt the Xml, therefore I have created a System.IO.MemoryStream, then I use this to create a System.IO.StreamWriter which I pass into my routine that creates the Xml. The problem is that only 1024 bytes (1Kb) seem to be written to the stream. I have tried creating the stream with a capacity of 102400 (100Kb) however only 1Kb is written. I have also tried setting the length of the stream to 100Kb, still only 1Kb is written and the 1025th byte in the stream is 0.

Here is a quick example of my code:
Code:
    ' Create a MemoryStream with a capacity of 100Kb
    Dim myStream As New MemoryStream(102400)
    ' Use this MemoryStream to create a StreamWriter
    Dim myWriter As New StreamWriter(myStream)

    ' Call the MakeXml routine and pass in the StreamWriter
    MakeXml(myWriter)

    ' Use the MemoryStream to create a StreamReader
    Dim myReader As New StreamReader(myStream)

    ' Read to the end of the StreamReader placing the data into a text box
    Text1.Text = myReader.ReadToEnd
    And here is an example of the code in the MakeXml() routine:
Code:
Public Sub MakeXml(ByRef s As TextWriter)
Code:
    Dim XmlWriter As New System.Xml.XmlTextWriter(s)

    With XmlWriter
        .WriteStartDocument()
        .WriteStartElement("Book")
        .WriteAttributeString("Title", "Pro VB.NET")

        ' Lots more calls to write some Xml content...

        .WriteEndElement()
        .WriteEndDocument()
    End With

End Sub
What do I have to do to write more than 1Kb to the MemoryStream? What am I doing wrong?

Regards
Owain Williams
__________________
Regards
Owain Williams
 
Old November 4th, 2004, 09:59 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 231
Thanks: 0
Thanked 0 Times in 0 Posts
Default

OK, it is just me being stupid, I have found the answer. Before I continue there was a mistake in the code I posted made when simplifying my actual code, I forgot to state that I set the position of the MemoryStream (the variable myStream) to 0 (myStream.Position = 0) before declaring the StreamReader.

Anyway, I fixed it by flushing the XmlTextWriter before returning from the MakeXml routine, here is my code again, this time working!:
Code:
    ' Create a MemoryStream with a capacity of 100Kb
    Dim myStream As New MemoryStream(102400)
    ' Use this MemoryStream to create a StreamWriter
    Dim myWriter As New StreamWriter(myStream)

    ' Call the MakeXml routine and pass in the StreamWriter
    MakeXml(myWriter)

    ' Position the MemoryStream at the start before creating the StreamReader
    myStream.Position = 0

    ' Use the MemoryStream to create a StreamReader
    Dim myReader As New StreamReader(myStream)

    ' Read to the end of the StreamReader placing the data into a text box
    Text1.Text = myReader.ReadToEnd
    Here is the fixed MakeXml() routine:
Code:
Public Sub MakeXml(ByRef s As TextWriter)
Code:
    Dim XmlWriter As New System.Xml.XmlTextWriter(s)

    With XmlWriter
        .WriteStartDocument()
        .WriteStartElement("Book")
        .WriteAttributeString("Title", "Pro VB.NET")

        ' Lots more calls to write some Xml content...

        .WriteEndElement()
        .WriteEndDocument()
        ' Don't forget to Flush the stream before exiting the routine!
        .Flush()
    End With

End Sub
And that was all I needed to do.

D'oh :)

Regards
Owain Williams





Similar Threads
Thread Thread Starter Forum Replies Last Post
filling RichTextBox using MemoryStream melvik C# 8 August 30th, 2011 02:25 AM
Writing Forums Teh_Archambizzle HTML Code Clinic 2 October 4th, 2006 07:22 PM
Help writing functions please Dwizz VB.NET 2002/2003 Basics 7 April 5th, 2005 11:05 AM
Help writing a SQL sac11585 SQL Server 2000 2 October 6th, 2004 02:16 PM
IO.MemoryStream from a string planoie ASP.NET 1.0 and 1.1 Professional 2 November 17th, 2003 05:44 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.