Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Getting Image Data From XML

Posted on 2004-08-25
Medium Priority
Last Modified: 2012-06-27
I have image data stored in an XML file using bin.base64 encoding. The xml file is as follows:
  <PhotoContent xmlns:dt="urn:schemas-microsoft-com:datatypes" dt:dt="bin.base64">/9j/4AAQSkZJRgABAgEBLAEsAAD/4RV2RXhpZgAA</PhotoContent>

i also have a user control which takes a byte() object as input and outputs an image. When i get the xml value from the xml file and pass it to this control, i get an error of "Invalid parameter used."

======  Image User Control
    Protected Friend Sub ImageByByte(ByVal sByte As Byte())
        If (Not sByte Is Nothing) Then
            Response.ContentType = "image/jpg"
            Dim iStream As New System.IO.MemoryStream(sByte, True)
            iStream.Write(sByte, 0, sByte.Length)
            sByte = Nothing

            Dim fullImg As Bitmap = New Bitmap(iStream)
            fullImg = System.Drawing.Image.FromStream(iStream)
            iStream = Nothing

            fullImg.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg)
            fullImg = Nothing
        End If
    End Sub

here's how i get the xml data and pass it to the control
    Private Function xmlPhoto(ByVal userName As String) As Byte()
        Dim xDoc As New XmlDocument(), xNode As XmlNode
        Dim asc As New System.Text.ASCIIEncoding()

            xDoc = New XmlDocument()
            xNode = xDoc.SelectSingleNode("//Photo[UserName = '" & userName & "']")
            xmlPhoto = asc.GetBytes(xNode.Item("PhotoContent").InnerText)
            asc = Nothing
            xDoc = Nothing
            xNode = Nothing
        End Try
    End Function

If i pass the byte data to the control directly from a sql table (where images are also stored) there's no problem. so i'm doing something wrong in grabbing the image data from xml.

xNode.Item("PhotoContent").InnerText returns the correct xml element value.
xmlPhoto.GetType returns System.Byte[].  
xmlPhoto.length also returns an integer value >  0.

Any help would be greatly appreciated. thanks.

Question by:jklNYC
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
  • 2
LVL 26

Expert Comment

ID: 11896962
Use nodeTypedValue, something more or less like:

            xNode = xDoc.SelectSingleNode("//Photo[UserName = '" & userName & "']")
            xmlPhoto = xNode.nodeTypedValue

Darshan Singh had a good article on doing this:

Mike Sharp

Author Comment

ID: 11897042

Yeah I've tried to use the nodetypedvalue and it doesn't work. i get this error on compile
"img.aspx.vb(82): 'nodetypedvalue' is not a member of 'System.Xml.XmlNode'."

I've read that article, and I am unable to use the following lines:

Dim oXMLDOMDoc As New MSXML2.DOMDocument40

The server won't recognize those object declarations. Am I missing something on the server? Do i need to install another XML library object?


LVL 26

Accepted Solution

rdcpro earned 1000 total points
ID: 11897251
Oh, I didn't know you were using .NET.  You might be able to use the XmlTextReader.ReadBase64 method, which reads a base64 encoding stream and fills a byte array buffer.  It returns the number of bytes written to the buffer.

Public Function ReadBase64( _
   ByVal array() As Byte, _
   ByVal offset As Integer, _
   ByVal len As Integer _
) As Integer


    The array of characters that serves as the buffer to which the text contents are written.
    The zero-based index into the array specifying where the method can begin to write to the buffer.
    The number of bytes to write into the buffer.
  Return Value
    The number of bytes written to the buffer.


Like ReadChars, this method can be called successively to read large streams of embedded text. It decodes Base64 content and returns the decoded binary bytes (for example, an inline Base64 encoded GIF image) into the buffer. See RFC 1521. (You can obtain RFCs from the Request for Comments Web site at

Here's a C# example that does basically the same thing:

XmlTextReader reader = new XmlTextReader(filename);
while(reader.Read()) {
  if (reader.LocalName == "element") {
    byte[] bytes = new byte[1000];
    int n = reader.ReadBase64(bytes, 0, 1000);
    string buf = Encoding.Unicode.GetString(bytes);

And, a VB.NET example from MSDN:

[Visual Basic, C#, C++] The following example reads a file containing Base64 and BinHex data.

[Visual Basic]
Option Explicit
Option Strict

Imports System
Imports System.IO
Imports System.Xml
Imports Microsoft.VisualBasic

Public Class Sample
   Private Const filename As String = "binary.xml"
   Public Shared Sub Main()
      Dim reader As XmlTextReader = Nothing
      Dim i As Integer

         reader = New XmlTextReader(filename)
         reader.WhitespaceHandling = WhitespaceHandling.None        

         ' Read the file. Stop at the Base64 element.        
         While reader.Read()
            If "Base64" = reader.Name Then
               Exit While
            End If
         End While

         ' Read the Base64 data. Write the decoded
         ' bytes to the console.
         Console.WriteLine("Reading base64... ")
         Dim base64len As Integer = 0
         Dim base64(1000) As Byte
            base64len = reader.ReadBase64(base64, 0, 50)
            For i = 0 To base64len - 1
            Next i
         Loop While (reader.Name = "Base64")

         ' Read the BinHex data. Write the decoded
         ' bytes to the console.
         Console.WriteLine(ControlChars.CrLf & "Reading binhex....")
         Dim binhexlen As Integer = 0
         Dim binhex(1000) As Byte
         binhexlen = reader.ReadBinHex(binhex, 0, 50)
            binhexlen = reader.ReadBinHex(binhex, 0, 50)
            For i = 0 To binhexlen - 1
            Next i
         Loop While (reader.Name = "BinHex")
         Console.WriteLine("Processing of the file {0} complete.", filename)
         If Not (reader Is Nothing) Then
         End If
      End Try
   End Sub 'Main
End Class 'Sample

Mike Sharp

Featured Post

How to Create Failover DNS Record Sets in Route 53

Route 53 has the ability to easily configure DNS record sets specifically for failover scenarios. These failover record sets can be configured to failover to full-blown deployments in other regions or to a static HTML page that informs your customers of the issue.

Question has a verified solution.

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

Introduction In my previous article ( I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

664 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