Apr 21, 2014

Serialize list of objects to xml file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;

namespace wmpplayer
{
    [XmlTypeAttribute(AnonymousType = true)]
    public class MediaLibData
    {
        [XmlElement("MediaLib")]
        public List<MediaLib> MediasLib { get; set; }

        public MediaLibData()
        {
            MediasLib = new List<MediaLib>();
        }
    }
    public class MediaLib
    {
        [XmlElement(ElementName = "Path")]
        public string Path { get; set; }

        [XmlElement(ElementName = "Name")]
        public string Name { get; set; }

        public MediaLib()
        {
            Path = string.Empty;
            Name = string.Empty;
          
        }
        public MediaLib(string opath, string oname)
        {
            Path = opath;
            Name = oname;
           
        }

    }


    [XmlTypeAttribute(AnonymousType = true)]
    public class UsersSetting
    {
        [XmlElement("UserSetting")]
        public List<UserSetting> settings { get; set; }
        public UsersSetting()
        {
            settings = new List<UserSetting>();
        }
    }
    public class UserSetting
    {       

        [XmlElement(ElementName = "Name")]
        public string Name { get; set; }

        [XmlElement(ElementName = "Volume")]
        public int Volume { get; set; }

        [XmlElement(ElementName = "Shuffle")]
        public bool Shuffle { get; set; }

        [XmlElement(ElementName = "AutoRewind")]
        public bool AutoRewind { get; set; }
        public UserSetting()
        {
            Name = string.Empty;
            Volume = 0;
            Shuffle = false;
            AutoRewind = false;
        }
        public UserSetting(string oname, int ovol, bool oshuf, bool orewind)
        {
            Name = oname;
            Volume = ovol;
            Shuffle = oshuf;
            AutoRewind = orewind;
        }

    }


    public class MediaLibrary
    {
        public void SerializerLib(MediaLibData obj, string PathFile)
        {
            try
            {
                using (TextWriter reader = new StreamWriter(PathFile))
                {
                    (new XmlSerializer(typeof(MediaLibData))).Serialize(reader, obj);
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        public MediaLibData DeserializeLib(string PathFile)
        {

            MediaLibData mysXmlClass = null;

            try
            {
                using (TextReader reader = new StreamReader(PathFile))
                {
                    object myXmlClass = (object)(new XmlSerializer(typeof(MediaLibData))).Deserialize(reader);
                    mysXmlClass = (MediaLibData)myXmlClass;
                }
            }
            catch (Exception e)
            {
                throw e;
            }

            return mysXmlClass;
        }
        public void SerializerSettings(UsersSetting obj, string PathFile)
        {
            try
            {
                using (TextWriter reader = new StreamWriter(PathFile))
                {
                    (new XmlSerializer(typeof(UsersSetting))).Serialize(reader, obj);
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        public UsersSetting DeserializeSetting(string PathFile)
        {

            UsersSetting mysXmlClass = null;

            try
            {
                using (TextReader reader = new StreamReader(PathFile))
                {
                    object myXmlClass = (object)(new XmlSerializer(typeof(UsersSetting))).Deserialize(reader);
                    mysXmlClass = (UsersSetting)myXmlClass;
                }
            }
            catch (Exception e)
            {
                throw e;
            }

            return mysXmlClass;
        }
    }
}
/**********************************************************************/
How to use:
- Load file
MediaLibrary lib = new MediaLibrary();
                if (File.Exists(userplpath))
                {
                    MediaLibData libData = new MediaLibData();
                    libData = lib.DeserializeLib(userplpath);
                    string[] filePaths=new string[libData.MediasLib.Count];
                    for (int i = 0; i < libData.MediasLib.Count; i++)
                    {
                        filePaths[i]=libData.MediasLib[i].Path;
                    }                   
                    myplayer.InitMedias(filePaths);
                    libData = null;                   
                }
- Write file
  MediaLibrary lib = new MediaLibrary();
            MediaLibData libData = new MediaLibData();
            foreach (MediaInfo item in myplayer)
            {
                libData.MediasLib.Add(new MediaLib(item.FullPath, item.FileName));
            }
            lib.SerializerLib(libData, userplpath);

0 comments:

Post a Comment

Nam Le © 2014 - Designed by Templateism.com, Distributed By Templatelib