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;

            }     
         
        }

    高山仰止, 景行行止。 四牡鲱鲱, 六辔如琴。 觏尔新婚, 以慰我心。
  • 相关阅读:
    试用第三方web推送GoEasy
    使用intellj idea 搭建本地开发环境
    一种基于struts2 拦截器 和 log4j的轻量级crm权限及行为跟踪方式
    Spring AOP声明式事务异常回滚 若干法则
    spring aop 切面测试
    centos 安装 mysql
    只是 换个方式,
    contrller 是 file's owners,
    色差,15,还是15 ,换了颜色 就显的小了,
    一块,分开,还是不分开 一个整体,
  • 原文地址:https://www.cnblogs.com/davidshi/p/3338572.html
Copyright © 2011-2022 走看看