Sunday, February 3, 2008

how to read binary file and save it into xml

Keywords: CDATA node of xml, FileStream, MemoryStream, Base64String


In xml document, you can define CDATA node to store some large text string. There is a constraint in xml document, the xml document only can contain text string.


How to save image data or other binary data into xml?


We can convert the binary data into Base64String data, then we can store the Base64String data into xml document. We can use the following approach to achive.


Approach1: BinaryFile===>Byte[]===>Base64String===>CDATA


The following is pseudo code:



byte[] buffer=File.ReadAllBytes(string path);


string base64String= Convert.ToBase64String();


XmlCDATA=new XmlCDATA(base64String);


XmlDocument.AddChild(XmlCDATA);



How to restore the Base64String data into a FileStream?


It is easy to reach. The procedure is just reverse the above process.

No comments: