序列化和反序列化

来源:互联网 发布:js字符串转unicode 编辑:程序博客网 时间:2024/06/05 19:37
using LibCommonUtil;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Serialization;


namespace VLCPlayer
{
    public class DeviceUtil
    {
        //private static string xmlDevDicPath = AppDomain.CurrentDomain.BaseDirectory + @"config";//存储文件路径
        private static string xmlDevFullPath = AppDomain.CurrentDomain.BaseDirectory + @"config\VlcDevices.xml";
        private static DeviceUtil m_DeviceUtil_Global = null;


        private List<DeviceInfo> devList = new List<DeviceInfo>();


        public delegate void DelegateAddDeviceOK(List<DeviceInfo> deviceList);
        public event DelegateAddDeviceOK OnDelegateAddDeviceOK;
        public static DeviceUtil GetDeviceUtil()
        {
            if (m_DeviceUtil_Global == null)
            {
                m_DeviceUtil_Global = new DeviceUtil();
            }
            return m_DeviceUtil_Global;
        }
        /// <summary>
        /// 从文件加载播放设备.成功返回0,其余非0
        /// </summary>
        /// <returns></returns>
        public int LoadDeviceFromXML()
        {
            if (!File.Exists(xmlDevFullPath))//判断文件是否存在
            {
                return 1;
            }


            TextReader reader = new StreamReader(xmlDevFullPath);
            try
            {
                XmlSerializer deserializer = new XmlSerializer(typeof(DeviceInfoList));


                object obj = deserializer.Deserialize(reader);
                DeviceInfoList XmlReadData = (DeviceInfoList)obj;
                reader.Close();
                if (XmlReadData.Devices != null)
                {
                    if (XmlReadData.Devices.Count > 0)
                    {
                        devList = XmlReadData.Devices;
                        return 0;
                    }
                    else 
                    {
                        return 5;
                    }


                }
                else
                {
                    return 4;
                }
            }
            catch (Exception)
            {
                reader.Close();
                return 3;
            }
            //return 2;
        }
        /// <summary>
        /// 保存设备设备.成功返回0,其余非0
        /// </summary>
        /// <param name="dev"></param>
        /// <returns></returns>
        public int SaveDeviceIntoXML(DeviceInfo dev)
        {
            foreach (DeviceInfo de in devList)
            {
                if (de.DevCode == dev.DevCode)
               {
                   return 1;
               }
            }
            devList.Add(dev);


            DeviceInfoList writeInfo = new DeviceInfoList();
            writeInfo.Devices = devList;
            XmlSerializer serializer = new XmlSerializer(typeof(DeviceInfoList));
            using (TextWriter writer = new StreamWriter(xmlDevFullPath))
            {
                serializer.Serialize(writer, writeInfo);
                writer.Close();
            } 
            //-----------------------------------------------------
            if (OnDelegateAddDeviceOK!=null)
            {
                OnDelegateAddDeviceOK(devList);
            }


            return 0;
        }
        /// <summary>
        /// 获取设备列表
        /// </summary>
        /// <returns></returns>
        public List<DeviceInfo> GetOnlineDevList()
        {
            LoadDeviceFromXML();
            return devList;
        }
    }
}
原创粉丝点击