Click here to Skip to main content
15,745,973 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi guys!
Big problem, small question.
So I'm working with xml. I create xml and one of the element is byte array.
I convert image to byte[]. Vs2010 create .xml I open it and everyting if fine. A have a long byte array (~30000 char).

Code snipet -> (item is struc)
byte[] bytePicArray = imageToByteArray(item.picture);
 xmlWriter.WriteBinHex(bytePicArray, 0, bytePicArray.Length);

private byte[] imageToByteArray(Image img)
           using (MemoryStream ms = new MemoryStream())
               img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
               return ms.ToArray();

The problem is How can I now read from saved xml file that byte array????
I trying to use ->
XmlReader.ReadContentAsBinHex Method

but no luck. I don't understand. Can someone give me some advice???
Thanks. :)

Why working at the low level of XmlWriter/XmlReader?

You can better of using Data Contract.


I advocated this great approach in my past solutions. Please see:
How can I utilize XML File streamwriter and reader in my form application?[^],
Creating a property files...[^].

Share this answer
Espen Harlinn 16-Jun-11 17:29pm    
Sensible approach, easy to implement - my 5
Sergey Alexandrovich Kryukov 16-Jun-11 21:26pm    
Thank you, Espen.
I would convert my byte array into Base64 and then serialize the base64 string to xml, something like this

void SavePic()

           Image img = Image.FromFile("ID001.jpg");
           using (MemoryStream ms = new MemoryStream())
               img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
               byte[] picArray = ms.ToArray();
               string picString = Convert.ToBase64String(picArray);
               XmlWriter writer = XmlWriter.Create("pic.xml");
               writer.WriteBase64(picArray,0, picArray.Length);

then to retrieve the image you do something like this (Convert.FromBase64String being the operative here)

void ViewPic()
            var x = XDocument.Load("pic.xml");
            string s = (string)x.Descendants("pic").First();
            byte[] imageBytes = Convert.FromBase64String(s);
            MemoryStream ms = new MemoryStream(imageBytes);
            pictureBox1.Image = Image.FromStream(ms, true);

Hope this helps
Share this answer
Reza Mansoori 16-Jun-11 10:35am    
Good Way
Sergey Alexandrovich Kryukov 16-Jun-11 14:57pm    
This is a right idea -- base64, my 5.
However I think my approach is better, please see my solution.
valza 17-Jun-11 2:07am    
so. Jeee this is a good way to solve my issue. :)
I chose to use write image as object to xml -> 5 from me
Hello Dear
some time a go I was working on A WPF-WCF Chat application , i wanted to save user information in a XML File at server side . user information includes user name , user family , user image and etc . to save and retrieve these information in a XML file i did easiest way : using DataSet and DataTable .
dataset has some usefull method formworking on XML
DataSet Dset = new DataSet();

DataTable Dtable = new DataTable();

i could save and retrieve data including image file , from XML file easily

use this methods , it works
Good Luck
Share this answer
Sergey Alexandrovich Kryukov 16-Jun-11 14:58pm    
Not sure...
I think my approach is better, please see my solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900