簡體   English   中英

具有自定義類的C#字典到XML文件,然后反向

[英]C# Dictionary with custom class to XML file, and reverse

我希望能夠在應用程序啟動時將xml文件加載到Dictionary(string,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;

...

那是我的課。 我嘗試了很多我可以認為是初學者的事情,但是沒有成功。

我將不勝感激任何建議。 謝謝!

XmlSerializer不直接支持Dictionary序列化,但是我們可以定義一組輔助方法以將其保存為列表,然后將其檢索到字典中,這是使用擴展方法和數據持有人類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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM