【问题标题】:C# Dictionary with custom class to XML file, and reverse具有自定义类的 C# 字典到 XML 文件,并反向
【发布时间】:2015-09-10 18:04:15
【问题描述】:

我希望能够在应用启动时将 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;

...

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

如果有任何建议,我将不胜感激。谢谢!

【问题讨论】:

  • 你的代码和字典有什么关系?我很乐意为您提供帮助,但我不明白您的问题。
  • 你需要编写一个类来序列化和反序列化你的字典。见Why isn't there an XML-serializable dictionary in .NET?
  • 嗯,这是我的问题。我有字典(字符串,学生),我想在哪里使用班级本身的 JMBG。我有一个 UI,您可以在其中输入所有这些内容并将其添加到字典中。但我希望能够将一个包含少数学生的 XML 加载到字典,然后在应用程序打开时将更多学生添加到 Dic 并保存新的字典到 XML。

标签: c# xml class dictionary


【解决方案1】:

XmlSerializer 不直接支持字典序列化,但我们可以定义一组辅助方法将其保存为列表,然后将其检索到字典中,这是一个使用扩展方法和数据持有者类的示例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>

【讨论】:

    猜你喜欢
    • 2014-11-13
    • 1970-01-01
    • 1970-01-01
    • 2016-10-15
    • 2015-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多