zoukankan      html  css  js  c++  java
  • 将图片字节数组存储到xml以及从xml读取

    public class RWPhotoXmlUtils
        {
            public  static void WriteXml(string foldName,string fileName,Dictionary<int, byte[]> dict)
            {
                try
                {
                    XElement root = new XElement("Root");
                    foreach (KeyValuePair<int, byte[]> obj in dict)
                    {
                        XElement photo = new XElement("Photo");
                        XElement riderId = new XElement("RiderId", obj.Key);                   
                        string str = System.Convert.ToBase64String(obj.Value);
                        XElement riderPhoto = new XElement("RiderPhoto", str);
                        photo.Add(riderId);
                        photo.Add(riderPhoto);
                        root.Add(photo);
                    }

                    string xmlString = root.ToString();
                    IsolatedStorageUtils.WriteDataToFile(foldName, fileName, xmlString);
                }
                catch (Exception ex)
                {
                    DebugUtils.Debug("Exception occured at RWPhotoXmlUtils.cs WriteXml method:", ex);
                }

            }

            public static Dictionary<int, byte[]> ReadXml(string fileName)
            {
                Dictionary<int, byte[]> dict = new Dictionary<int, byte[]>();
                try
                {
                 
                    string xmlString = IsolatedStorageUtils.ReadDataFromFile(fileName);
                    XElement xml = XElement.Parse(xmlString);            

                    var myCollection = from Photo in xml.Descendants("Photo")
                             select new
                             {
                                 RiderId = Photo.Element("RiderId").Value,
                                 RiderPhoto = Photo.Element("RiderPhoto").Value
                             };

                    foreach (var obj in myCollection)
                    {
                       byte[] bytes =  System.Convert.FromBase64String(obj.RiderPhoto);
                        dict.Add(Int32.Parse(obj.RiderId),  bytes);
                        
                    }


                }
                catch (Exception ex)
                {
                    DebugUtils.Debug("Exception occured at RWPhotoXmlUtils.cs ReadXml method:", ex);
                    
                }
                return dict;

            }     
         
        }

    高山仰止, 景行行止。 四牡鲱鲱, 六辔如琴。 觏尔新婚, 以慰我心。
  • 相关阅读:
    使用SetTimer函数为Delphi的Win控件设置时钟
    关于QT版本的安装配置的一些困惑
    Linux设备驱动开发详解-Note(11)--- Linux 文件系统与设备文件系统(3)
    邪恶的C++
    TControl的主要功能研究(属性,函数,事件)
    Delphi研究,对全局变量函数与OOP编程关系的一点体会 good
    QT 相关资源(书籍、论坛、博客等。。。)整理
    VC UI界面库大集合
    .net程序员求职简历
    C++著名程序库的比较
  • 原文地址:https://www.cnblogs.com/davidshi/p/3338572.html
Copyright © 2011-2022 走看看