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);
}