source: extensions/PiwigoLib/PiwigoWpf/Helper/XMLHelper.cs @ 11911

Last change on this file since 11911 was 11911, checked in by bayral, 13 years ago

WPF inprovement

File size: 1.9 KB
Line 
1using System;
2using System.Xml;
3using System.IO;
4
5namespace Com.Piwigo.Wpf.Helper
6{
7    public static class XMLHelper<T>
8    {
9        /// <summary>
10        ///  return object from xml retuned by piwigo
11        /// </summary>
12        /// <param name="xmlData"></param>
13        /// <returns></returns>
14        internal static T DeserializeObjectFromFile(FileInfo xmlFile)
15        {
16            T ReturnValue = default(T);
17            try
18            {
19                if (xmlFile.Exists)
20                {
21                    System.Xml.Serialization.XmlSerializer xsSerializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
22                    ReturnValue = (T)xsSerializer.Deserialize(xmlFile.OpenRead());
23                }
24            }
25            catch (Exception ex)
26            {
27                throw new ApplicationException (
28                            String.Format(
29                                "The file {0} can't be deserialized in {1}, the error is {2} .",
30                                xmlFile.FullName, typeof(T).Name, ex.Message),
31                            ex);
32            }
33
34            return ReturnValue;
35        }
36
37        internal static void SerializeToXMLFile(T obj, FileInfo xmlFile)
38        {
39            try
40            {
41                if (xmlFile.Exists) xmlFile.Delete();
42                System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
43                serializer.Serialize(xmlFile.CreateText(), obj);
44            }
45            catch (Exception ex)
46            {
47                throw new ApplicationException(
48                            String.Format(
49                                "The value {0} can't be serialized in file {1}, the error is {2} .",
50                                obj.ToString(), xmlFile.FullName, ex.Message),
51                            ex);
52            }
53        }
54    }
55}
Note: See TracBrowser for help on using the repository browser.