Trouble Writing Large Text File

  • 13 years ago

    Hi,

    I'm having trouble writing all of a large text stream to file for temporary storage. I'm trying to extract a pdf encoded in MIME's base 64 from an xml file. Most of the text appears but not all. I think my problem may be due to a limited capacity of the StreamWrtier class but I'm not sure. Here is my code:

             Public Function readPDF(ByVal filename As String) As Boolean
                Dim d As New XmlDocument()
                Dim sw As New StreamWriter("/temp.txt")
                d.Load(filename)
                Dim nl As XmlNodeList
                nl = d.GetElementsByTagName("Formatted-Report")
                sw.Write(nl(0).InnerText)
                sw.Close()
                Return True
            End Function

     Does anyone know of a better way to write large amounts of text?

  • 12 years ago

    I don't know whether you would prefer to write the output as XML as it would be just as simpler to do so, and you can do more with the xml document that you can with text; reading and writing text documents is quite processor intensive. Anyway, this runs very fast and does not lose data. I used a 36kb text file and took less than a second to complete. The xml returns the following company's orders from Northwind outputted using FORXML:

        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
            Dim str As System.IO.FileStream = New System.IO.FileStream("temp.xml", IO.FileMode.OpenOrCreate, IO.FileAccess.ReadWrite, IO.FileShare.ReadWrite)
    
            Dim settings As XmlWriterSettings = New XmlWriterSettings
            settings.ConformanceLevel = ConformanceLevel.Fragment
            settings.Indent = True
            settings.IndentChars = "     "
    
            Dim reader As XmlReader = XmlReader.Create("Order_out.xml")
            reader.MoveToContent()
            Dim writer As XmlWriter = XmlWriter.Create("NewOrders.xml", settings)
    
            While reader.Read()
                If reader.NodeType = XmlNodeType.Element And (reader.Name = "CustomerID") Then
                    writer.WriteElementString(reader.Name, reader.ReadInnerXml)
                    reader.ReadToFollowing("CompanyName")
                    If reader.NodeType = XmlNodeType.Element And (reader.Name = "CompanyName") Then
                        writer.WriteElementString(reader.Name, reader.ReadInnerXml)
                    End If
                End If
            End While
            writer.Flush()
            writer.Close()
            reader.Close()
        End Sub
    
    And this was the formatted output, the tags have been stripped by developerfusion;
    CHOPS
    Chop-suey Chinese
    DROGA
    Chop-suey Chinese
    GELLY
    Chop-suey Chinese
    CHOPS
    Chop-suey Chinese
    JUNKS
    Chop-suey Chinese
    KLINE
    Chop-suey Chinese
    LEHAY
    Chop-suey Chinese
    NOJSK
    Chop-suey Chinese
    PLESK
    Chop-suey Chinese
    QUAIT
    Chop-suey Chinese
    RIBST
    Chop-suey Chinese
    RYOPS
    Chop-suey Chinese
    SEDUL
    Chop-suey Chinese
    STARC
    Chop-suey Chinese
    TERRY
    Chop-suey Chinese
    VEGRY
    Chop-suey Chinese
    

     

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil.” - Donald Knuth