Save a binary file into an XML document

I couldn’t find a way to save an image file into an XML document so through trial and error I came up with this:

  1. Convert the image file to a base64 string
    Dim b() As Byte = IO.File.ReadAllBytes("filename.jpg")
    Dim s As String = Convert.ToBase64String(b)
  2. Create a CDATA section in the XML file (doc is the Xmldocument)
     Dim cd As XmlCDataSection
    Dim level1 as Xmlelement = doc.CreateElement("codesection")
    cd = doc.CreateCDataSection(s)
    level1.AppendChild(cd)

Leave a Reply

Your email address will not be published. Required fields are marked *