public class yzzSerialize { private yzzSerialize() { } private static yzzCache cache = new yzzCache(); public static T GetFromXml<T>(string xmlpath, T t) { using (FileStream fs = new FileStream(xmlpath, FileMode.Open, FileAccess.Read)) { Type type = typeof(T); XmlSerializer xs = cache[type.FullName] as XmlSerializer; if (xs == null) { xs = new XmlSerializer(type); cache[type.FullName] = xs; } t = (T)xs.Deserialize(fs); fs.Close(); return t; } } public static void SetToXml<T>(string xmlpath, T t) { if (t == null) return; using (FileStream fs = new FileStream(xmlpath, FileMode.Create, FileAccess.Write)) { Type type = typeof(T); XmlSerializer xs = cache[type.FullName] as XmlSerializer; if (xs == null) { xs = new XmlSerializer(type); cache[type.FullName] = xs; } xs.Serialize(fs, t); t = default(T); fs.Close(); } } }