XML序列化与反序列化

将Object序列化为XML文件
[Serializable]
public class Person
{
    public string Name{get;set;}
    public bool Sex{get;set;}
}

class Porgram
{
    public static void SerializeToXml<T>(object obj,string path)
    {
        FileStream xmlFile = new FileStream(path,FileMode.OpenOrCreate);
        XmlSerializer xml = new XmlSerializer(typeof(T));
        try
        {
            xml.Serialize(xmlFile,obj);
            xmlFile.Close();
        }catch
        {
            throw;
        }
    }
    static void Main(string[] args)
    {
        Person a = new Person();
            a.name = "Gets";
            a.age = false;
            SerializeToXml<Person>(a,$"D:/123.xml");
    }
}

将XML文件转反序列化成Object对象
class Porgram
{
    public static T DeserializeToObject<T>(string path)
    {
        try
        {
            FileStream xmlFile = new FileStream(path, FileMode.Open);
            XmlSerializer xml = new XmlSerializer(typeof(T));

            T t = (T)xml.Deserialize(xmlFile);
            xmlFile.Close();
            return t;
        }catch
        {
            throw;
        }
    }
    static void Main(string[] args)
    {
        var a = DeserializeToObject<Person>($"D:/123.xml");
    }
}

XML文件的序列化和反序列化
public class Student: SerializableConfig
{
    [XmlAttribute(Namespace = "http://www.cpandl.com")]
    public string GroupName;

    [XmlAttribute(DataType = "base64Binary")]
    public Byte[] GroupNumber;

    [XmlAttribute(DataType = "date", AttributeName = "CreationDate")]
    public DateTime Today;

    [XmlAttribute]
    public string Name { get; set; }

    public List<College> Coll { get; set; }

    public Student()
    {
        Coll = new List<College>();
    }

       
}

public class College
{
    [XmlAttribute]
    public string ID { get; set; }
    [XmlAttribute]
    public string Age { get; set; }

}

//---------------------------------------------------------------------

 static void Main(string[] args)
{
    Student st = new Student();
    st.GroupName = ".Net";

    Byte[] hexByte = new Byte[2] { Convert.ToByte(100), Convert.ToByte(50) };
            st.GroupNumber = hexByte;

    DateTime date = new DateTime(2001, 1, 1);
    st.Today = date;

    st.Name = "Kkarot";
    st.Coll.Add(new College() { Age = "11", ID = "10245412" });
    st.Coll.Add(new College() { Age = "KK", ID = "CC" });


    st.SerializeObject("kakarot.xml");

    Student s = new Student();
    s.Load("kakarot.xml");
    Console.ReadKey();
}

SerializableConfig类
public class SerializableConfig
{
    public void Load(string filename)
    {
        Stream fstream = new FileStream(filename, FileMode.Open);
        XmlSerializer xml = new XmlSerializer(GetType());
        var obj = xml.Deserialize(fstream);

        PropertyInfo[] pros = this.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
        // 表示当前 System.Type 的匹配指定绑定约束的所有属性的 System.Reflection.PropertyInfo 对象数组

        foreach (var p in pros)
        {
            if (p.CanWrite && p.GetCustomAttribute<XmlIgnoreAttribute>() == null)
            {
                var obj_item = p.GetValue(obj); //返回指定对象的属性值
                p.SetValue(this, obj_item); //设置指定对象的属性值
            }
        }
        fstream.Close();
    }


    public void SerializeObject(string filename)
    {
        XmlSerializer my = new XmlSerializer(GetType());
        TextWriter writer = new StreamWriter(filename);

      
        my.Serialize(writer, this);
        writer.Close();
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值