So, this example comes right from MSDN. http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.readelementcontentasbase64.aspx
Pretty much the only thing I have changed is created a loop because I have one xmlstring and want to break it up and create tif files from it(which it actually does do).
My problem is this: readbytes inside the while loop throws an exception at the last time through the loop that says I have an “unexpected end of file; the following elements are not closed” and lists them
Public Shared Sub Base64DecodeImageFile()
Dim buffer(999) As Byte
Dim readBytes As Integer = 0
Using reader As XmlReader = XmlReader.Create(xmlstring)
Dim outputFile As New FileStream("C:\artFiles\data\newImage.jpg", FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write)
' Read to the image element.
reader.ReadToFollowing("image")
' Read the Base64 data.
Console.WriteLine(vbCr + vbLf + "Reading Base64...")
Dim bw As New BinaryWriter(outputFile)
readBytes = reader.ReadElementContentAsBase64(buffer, 0, 50)
While (readBytes > 0)
bw.Write(buffer, 0, readBytes)
readBytes = reader.ReadElementContentAsBase64(buffer, 0, 50)
End While
outputFile.Close()
End Using
End Sub 'Base64DecodeImageFile
So there are tags left open after the readtofollowing(“image”). I tried catching the exception to no avail, tried to read until eof, no avail. I dont really need elements to be closed, I just need to continue because I created files using whats in the image tag.
I appreciate any help!
EDIT: I think my xmlstring is going to have times where it will be too big to do a parse without slowing down too much…/parsing binary data?
Well! I found out my issue..I figured I would post the answer anyway to maybe help some others? the problem did not lay with the reader, in fact, it worked really well. The problem was the xmlstring itself. The string never created the closing tags I needed (which would happen the last loop through).
Thanks for your help anyway guys!