• 将图片字节数组存储到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;

            }     
         
        }

    高山仰止, 景行行止。 四牡鲱鲱, 六辔如琴。 觏尔新婚, 以慰我心。
  • 相关阅读:
    算法导论 第一章
    20155312 2016-2017-2 《Java程序设计》第七周学习总结
    Visual Studio 2005 搭建Windows CE 6.0环境之准备
    C#在winform中调用系统控制台输出
    C# 目录(文件夹)复制实现
    关于加强数据库安全的一些实践
    运维小白部署网站踩坑全过程
    jQuery学习之二 jQuery选择器
    运维系列之二 Linux文件种类和扩展名
    运维系列之一 Linux的文件与目录权限解析
  • 原文地址:https://www.cnblogs.com/davidshi/p/3338572.html
Copyright © 2020-2023  润新知