65.9K
CodeProject is changing. Read more.
Home

Passing binary data in xml: a C# example which puts and gets icon and bitmaps to and from a xml file.

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.27/5 (11 votes)

Jun 14, 2001

1 min read

viewsIcon

201339

downloadIcon

4198

Binary data needs to be base64 encoded to go into an xml file. .Net framework has made this a painless method wiht its System.Xml namespce classes. I use the XmlTextWriter to put in a bitmap and icon image into a xml file. I then use XmlTextReader to parse the xml file and dynamically set the icon a

Sample Image - screen_shot_Genx.jpg

Introduction

Looking at the .resx file that VS.NET uses to store resources such as images, I thought wouldn't it be cool to have a XML file where you can store images and other info for you app and at startup your app gets the look and feel from the XML file dynamically. You can then change the look and feel by changing the XML file, maybe over the internet! A little investigation showed that .NET namespace System.XML has 2 great classes XMLTextWriter and XMLTextReader which does all the hard work for you with their Read/WriteBase64 methods.

m_XMLWriter.WriteStartElement("",NodeName,"");

<code>m_XMLWriter.WriteBase64(ImageBinaryBuffer,0,ImageBinaryBuffer.Length);

m_XMLWriter.WriteEndElement();

So here is my quick and dirty app, which uses 2 classes CLookXMLReader and CLookXMLWriter to do the putting in the binary and getting the binary out to bitmap or an icon. You basically use the Writer to put the images into the XML, then the reader can be used at a separate app to get the images out from the XML. Made the code a bit messy when reading the binary out from the XML, there must be a better way to it, please let me know! I hope you dont judge coding skills by just looking at this app only (I am usually much better!) :)