日期:2014-05-18  浏览次数:20960 次

求C# 对XML序列化与反序列化实例
求C# 对XML序列化与反序列化实例

最好能加点注释,谢谢了。

------解决方案--------------------
http://msdn.microsoft.com/zh-cn/library/system.xml.serialization.xmlserializer(v=VS.80).aspx
------解决方案--------------------
C# code

namespace ObjectsLibrary {
    public interface ObjectSerializer {
        string Serialize<T>(T obj);
        string Serialize(Type t, object obj);
        T Deserialize<T>(string content);
        object Deserialize(Type t, string content);
    }

    public class JsonObjectSerializer : ObjectSerializer {
        public string Serialize<T>(T obj) {
            return Serialize(typeof(T), obj);
        }

        public string Serialize(Type t, object obj) {
            string result = string.Empty;
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
            using (MemoryStream ms = new MemoryStream()) {
                serializer.WriteObject(ms, obj);
                result = Encoding.UTF8.GetString(ms.ToArray());
            }
            return result;
        }

        public T Deserialize<T>(string content) {
            return (T)Deserialize(typeof(T), content);
        }

        public object Deserialize(Type t, string content) {
            object result = null;
            using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(content))) {
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(t);
                result = serializer.ReadObject(ms);
            }
            return result;
        }
    }

    public class XmlObjectSerializer : ObjectSerializer {
        public string Serialize<T>(T obj) {
            return Serialize(typeof(T), obj);
        }

        public string Serialize(Type t, object obj) {
            StringBuilder result = new StringBuilder();
            try {
                XmlSerializer xs = new XmlSerializer(t);
                xs.Serialize(new StringWriter(result), obj);
            } catch (Exception ex) {
                Console.WriteLine(ex.Message);
            }
            return result.ToString();
        }

        public T Deserialize<T>(string content) {
            return (T)Deserialize(typeof(T), content);
        }

        public object Deserialize(Type t, string content) {
            object result = null;
            XmlSerializer xs = new XmlSerializer(t);
            result = xs.Deserialize(new StringReader(content));
            return result;
        }
    }
}

------解决方案--------------------
临时 写的,LZ,你看看行吗?

定义一个 路径: String path = "Person.txt";

序列化时:

//----序列化方法
public void SaveInfo()
{
//定义文件流
FileStream fs = new FileStream(Path,FileMode.Create);
//二进制方式
BinaryFormatter bf = new BinaryFormatter();
//序列化存储对象
bf.Serialize(fs,lst);
fs.Close();
}

反序列化时:

//判断文件路径是否存在
if (!File.Exists(Path))
{
return;
}
FileStream fs = new FileStream(Path, FileMode.Open);
BinaryFormatter bf = new BinaryFormatter();
this.lst = bf.Deserialize(fs) as List<Person>;
fs.Close();