c#字典与自定义类到XML文件,并反转

本文关键字:文件 XML 字典 自定义 | 更新日期: 2023-09-27 18:12:07

我希望能够在应用程序启动时加载xml文件到字典(字符串,customClass),并使用UI向其添加新元素。最后,我想保存由新旧元素组成的新xml。

[Serializable]
public class Student
{
    public string ime;
    public string prezime;
    public DateTime datumRodjenja;
    public string mestoRodjenja;
    public string JMBG;
    public string brojIndeksa;
    public string adresa;
    public string grad;
    public string brojTelefona;
    public string brojMobilnog;

那是我的课。作为一个初学者,我已经尝试了许多我能想到的事情,但都没有成功。

我将感谢任何建议。谢谢!

c#字典与自定义类到XML文件,并反转

XmlSerializer不直接支持字典序列化,但是我们可以定义一组辅助方法来将其保存为列表,然后将其检索到字典中,这里是使用扩展方法和数据holder类Entry的示例

public static class Helper
{
    public static void Load<TK, TV>(this Dictionary<TK, TV> dic, Stream stream)
    {
        var reader = new XmlSerializer(typeof (List<Entry<TK, TV>>));
        var list = (List<Entry<TK, TV>>)reader.Deserialize(stream);
        foreach(var l in list)
            dic.Add(l.Key,l.Value);
    }
    public static void Save<TK, TV>(this Dictionary<TK, TV> dic, Stream stream)
    {
        var list = new List<Entry<TK, TV>>();
        foreach (var pair in dic)
            list.Add(new Entry<TK, TV> {Key = pair.Key, Value = pair.Value});
        var writer = new XmlSerializer(typeof(List<Entry<TK, TV>>));
        writer.Serialize(stream, list);
    }
    public static void Load<TK, TV>(this Dictionary<TK, TV> dic, string path)
    {
        using(var f=File.OpenRead(path))
            Load(dic,f);
    }
    public static void Save<TK, TV>(this Dictionary<TK, TV> dic, string path)
    {
        using (var f = File.OpenWrite(path))
            Save(dic, f);
    }
}
public class Entry<TK, TV>
{
    [XmlAttribute]
    public TK Key { get; set; }
    public TV Value { get; set; }
}

,这里有一个用法示例:

public class Student
{
    public string ime { get; set; }
    public string prezime { get; set; }
    public DateTime datumRodjenja { get; set; }
    public string mestoRodjenja { get; set; }
    public string JMBG { get; set; }
    public string brojIndeksa { get; set; }
    public string adresa { get; set; }
    public string grad { get; set; }
    public string brojTelefona { get; set; }
    public string brojMobilnog { get; set; }
}
class Program
{
    static void Main(string[] args)
    {
        var dic=new Dictionary<string,Student>();
        var student1 = new Student
        {
            adresa = "adresa",
            JMBG = "1234",
            prezime = "prezime",
            datumRodjenja = DateTime.Now
        };
        var student2= new Student
        {
            adresa = "adresa",
            JMBG = "4567",
            prezime = "prezime",
            datumRodjenja = DateTime.Now
        };
        dic.Add(student1.JMBG,student1);
        dic.Add(student2.JMBG,student2);
        dic.Save("test.xml"); //save sample
        dic.Clear();
        dic.Load("test.xml"); //load sample
        var student3 = new Student
        {
            adresa = "adresa",
            JMBG = "9012",
            prezime = "prezime",
            datumRodjenja = DateTime.Now
        };
        dic.Add(student3.JMBG,student3); //adding new student 
            dic.Save("test.xml"); //saving again to add recently added student
    }

和最后的test.xml

<?xml version="1.0"?>
<ArrayOfEntryOfStringStudent xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <EntryOfStringStudent Key="1234">
    <Value>
      <prezime>prezime</prezime>
      <datumRodjenja>2015-09-10T23:19:20.6226745+04:30</datumRodjenja>
      <JMBG>1234</JMBG>
      <adresa>adresa</adresa>
    </Value>
  </EntryOfStringStudent>
  <EntryOfStringStudent Key="4567">
    <Value>
      <prezime>prezime</prezime>
      <datumRodjenja>2015-09-10T23:19:20.6266749+04:30</datumRodjenja>
      <JMBG>4567</JMBG>
      <adresa>adresa</adresa>
    </Value>
  </EntryOfStringStudent>
  <EntryOfStringStudent Key="9012">
    <Value>
      <prezime>prezime</prezime>
      <datumRodjenja>2015-09-10T23:19:20.722681+04:30</datumRodjenja>
      <JMBG>9012</JMBG>
      <adresa>adresa</adresa>
    </Value>
  </EntryOfStringStudent>
</ArrayOfEntryOfStringStudent>