1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
|
using System;
using System.Xml.Serialization;
using System.IO;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace DSI
{
public class XMLSerialize<T> : MarshalByRefObject where T : new()
{
// Le serialiser est statique ainsi il n'est construit qu'une fois par le CLR
// gain de temps enorme qui evite de faire une reflection à chaque utilisation
static XmlSerializer m_serializer = new XmlSerializer(typeof(T));
static public T LoadFromFile(string fileName)
{
T result;
try
{
using (FileStream input = new FileStream(fileName, FileMode.Open))
{
result = (T)m_serializer.Deserialize(input);
}
}
catch
{
result = new T();
}
return result;
}
static public T LoadFromString(string data)
{
T result;
try
{
using (StringReader sr = new StringReader(data))
{
result = (T)m_serializer.Deserialize(sr);
}
}
catch
{
result = new T();
}
return result;
}
static public T LoadFromStream(Stream stream)
{
T result;
try
{
XmlSerializer read = new XmlSerializer(typeof(T));
result = (T)m_serializer.Deserialize(stream);
}
catch
{
result = new T();
}
return result;
}
public void SaveToStream(Stream stream)
{
m_serializer.Serialize(stream, this);
}
public string SaveToString()
{
StringWriter sw = new StringWriter();
m_serializer.Serialize(sw, this);
return sw.ToString();
}
public void SaveToFile(string fileName)
{
using (StreamWriter sm = new StreamWriter(fileName))
{
m_serializer.Serialize(sm, this);
}
}
}
//Ma classe de serialisation
public class XmlDSI : XMLSerialize<XmlDSI>
{
public XmlDSI()
{
}
public XmlDSI(DSI_Patient dsi)
{
DSI = dsi;
}
public DSI_Patient DSI;
}
} |
Partager