Adding base64 encoded image in XML using Linq in C#

This example shows you how to read and convert a file from a file location into base64 string with help of linq in order that that file be included as a attribute in a xml document. For more info on how to interact with linq and xml creation check my post: CREATING XML DOCUMENT USING LINQ IN CSHARP

Solution:

string SaveXMLLocation = @"C:\directory\XmlDocument.xml";
string ImageFileLocation = @"C:\directory\SomePicture.jpeg";

byte[] oFileBytes = null;
if (System.IO.File.Exists(ImageFileLocation))
{
    oFileBytes = System.IO.File.ReadAllBytes(ImageFileLocation);
}

var xmlDocument =
new XElement("content", new XAttribute("contentencoding", "base64"),
    new XAttribute("contenttype", "image/jpeg"),                                                        
    new XCData(Convert.ToBase64String(oFileBytes, Base64FormattingOptions.InsertLineBreaks))
);

xmlDocument.Save(SaveXMLLocation);

Output:

<?xml version="1.0" encoding="utf-8"?>
<content contentencoding="base64" contenttype="image/jpeg">
<![CDATA[/9j/4AAQSkZJRgABAQEAYABgAAD/4QBkRXhpZgAATU0AKgAAAAgABQESAAMAAAABAAEAAAExAAIA
AAARAAAASlEQAAEAAAABAQAAAFERAAQAAAABAAAAAFESAAQAAAABAAAAAAAAAABBZG9iZSBJbWFn
ZVJlYWR5AAD/2wBDAAIBAQIBAQICAgICAgICAwUDAwMDAwYEBAMFBwYHBwcGBwcICQsJCAgKCAcH
Cg0KCgsMDAwMBwkODw0MDgsMDAz/2wBDAQICAgMDAwYDAwYMCAcIDAwMDAwMDAwMDAwMDAwMDAwM
DAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAz/wAARCABmANQDASIAAhEBAxEB/8QAHwAA
AQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIh
MUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpT
VFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5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xBb/APWdNa+tbzWZNQSSDWL2N7i
aCMQyExbC2wqTyeM5wQRgCvNNRvvHkGpwl/Flqi+cFCQaasYbjOGYNuwc+uc0UV4uXxjGtXaS1d9
Un081p8j2s2xVaWEw15vRW3fR/j8z7S/Yv09dWt4bzWtB0zULeO8n8yRNUuYpXZIWDLsAK+X90qo
YFSM5JrzH9sTxhceJUjWHTdP0T7OkVxBJp9xMkqO+AwY5+ZSmRxt5JJz0oor8/y3DxeeTm73i9NX
1fr/AF8z9GzzEVI5JDlekoq66demyem6Rg/sU+BrXxz45W11ObUtUht7IESXWp3azW++aJd0TRyq
A2doy6uNuRjJzX7O/sSfs8/C1/gt4u1S+8MXN0vnXDmFrlpPsy/N8sTM2RjsT0oor36cpVOI3Co3
JRi2k9Vflb2em+uq0PjcVenw0qtP3ZSkk2tNOZdrHwh8e/Avh3wj8XvD+5dX1DT2lluTby3LRh2V
5GjVgjAMqiHbyCDvOVOMHoP2xtH+E+jeMfiDp9j8PIbWW2toI9NeK8uIRZu0hBYbZsfdHoeaKK8j
ONMzpQjorL8n/XrZ9Ec0E5ZFUnJu931f80PP7u2tt2fmz4y8A65e63L9l8RarZws+FC3SttB56NE
emcUsfwj8Qamv2eTxdqixyDBMfkxvzz95Yc9utFFfoVTESjSi0lt/LH/ACPj8PSTerf3v/M6m2+B
Wp+SuPHHiyAYHyR3MZUcAfxRE89fxooor5546tfp/wCAx/yPsaeDp8q3/wDApf5n/9k=]]></content>

What you may be forgetting:

1.Don’t forget to include the Xml.Linq libraries and assemblies at the top of the .cs file

using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;

2. Make sure you have read write privileges in the specified Saving Location and that the Folder is already created. (Folder name ‘directory‘ for this example in ‘C:\‘)

 

This example was done in VS 2012 MVC4 web application

Leave a Reply

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