技能编辑器中使用xml格式序列化技能数据

 public class Person
    {
        public string name;
    }

    public class Student
    {
        public string name;
        public int age;
    }

    public class SkillData
    {
        public List<Person> persons;
        public List<Student> students;
    }

    class Program
    {
        static void Main(string[] args)
        {
            //string url = string.Format("https://cs-sandbox.dailygn.com?role_id={0}&server_id={1}", "123", 10.ToString());

            //DLL dll = new DLL();
            //int age = dll.GetBirthYear();

            Person p1 = new Person();
            p1.name = "abc";

            SkillData skillData = new SkillData();
            skillData.persons = new List<Person>();
            skillData.persons.Add(p1);
            skillData.persons.Add(p1);
            //skillData.students = new List<Student>();
            string pathwithname = "H:/a.xml";

            XmlSerializer _formatter = new XmlSerializer(typeof(SkillData));
            using (FileStream writer = new FileStream(pathwithname, FileMode.Create))
            {
                //using Encoding
                StreamWriter sw = new StreamWriter(writer, Encoding.UTF8);
                XmlSerializerNamespaces xsn = new XmlSerializerNamespaces();
                //empty name spaces
                xsn.Add(string.Empty, string.Empty);
                xsn.Add("books", "http://www.cpandl.com");
                xsn.Add("money", "http://www.cohowinery.com");

                _formatter.Serialize(sw, skillData, xsn);
            }
        }
    }

在这里插入图片描述
如果是:skillData.students = new List();
在这里插入图片描述
如果是:
//xsn.Add(“books”, “http://www.cpandl.com”);
//xsn.Add(“money”, “http://www.cohowinery.com”);
在这里插入图片描述

反序列化:

 string pathwithname = "H:/a.xml";
 using (FileStream reader = new FileStream(pathwithname, FileMode.Open))
  {
      //IFormatter formatter = new BinaryFormatter();
      System.Xml.Serialization.XmlSerializer formatter = new System.Xml.Serialization.XmlSerializer(typeof(SkillData));
      SkillData skillData = (SkillData)formatter.Deserialize(reader);
  }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值