Click here to Skip to main content
15,907,687 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)
MIDL
xmlWriter.WriteStartElement("PictureByteArray");
byte[] bytePicArray = imageToByteArray(item.picture);
 xmlWriter.WriteBinHex(bytePicArray, 0, bytePicArray.Length);


C#
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. :)
Posted

Why working at the low level of XmlWriter/XmlReader?

You can better of using Data Contract.

See:
http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].

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...[^].

—SA
 
Share this answer
 
Comments
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.
--SA
I would convert my byte array into Base64 and then serialize the base64 string to xml, something like this

C#
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.WriteStartDocument();
               writer.WriteStartElement("pic");
               writer.WriteBase64(picArray,0, picArray.Length);
               writer.WriteEndElement();
               writer.WriteEndDocument();
               writer.Close();
           }
       }


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

C#
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
 
Comments
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.
--SA
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();
Dset.ReadXml("FilePath");
Dset.ReadXmlSchema("FilePath");
Dset.WriteXml("FilePath");
Dset.WriteXmlSchema("FilePath");

DataTable Dtable = new DataTable();
Dtable.ReadXml("FilePath");
Dtable.ReadXmlSchema("FilePath");
Dtable.WriteXml("FilePath");
Dtable.WriteXmlSchema("FilePath");


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

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

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