日期:2014-05-18 浏览次数:20855 次
using System.IO; using System.Xml.Serialization; namespace console1 { class Program { public static void Serialize(object obj, string path) { using (FileStream fileStream=new FileStream(path,FileMode.Create,FileAccess.Write)) { XmlSerializer xmlSerializer = new XmlSerializer(obj.GetType()); xmlSerializer.Serialize(fileStream,obj); } } public static T Deserialize<T>(string path) { using (FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read)) { XmlSerializer xmlSerializer = new XmlSerializer(typeof(T)); return (T)xmlSerializer.Deserialize(fileStream); } } static void Main(string[] args) { string path="C:\\1.xml"; byte[] bt = new byte[]{1,2,3,4,5}; Serialize(bt,path); byte[] bt1 = Deserialize<byte[]>(path); Console.WriteLine("Over"); Console.ReadKey(); } } }