【发布时间】:2020-07-01 03:57:33
【问题描述】:
所以,我最近有机会使用一种技术,由于没有更好的术语,我将其称为“Matroyshka Class”,以俄罗斯嵌套娃娃命名。该类有一个 List 属性,其中包含同一类的实例,每个实例也有一个类似的列表,或多或少是任意的“深度”。
下面是一个简化的示例:
class Doll
{
public string Color;
public List<Doll> ChildDolls;
// End of properties. Getters and Setters not included for readability.
public Doll(string color)
{
this.Color = color;
this.ChildDolls = new List<Doll>();
} // End of Constructor
public void AddChild(Doll NewChild)
{
this.ChildDolls.Add(NewChild);
} // End of Add Child method
public override string ToString()
{
string output;
// Adds the current doll's color
output += this.Color + "\n";
// Adds each doll's children, and each of theirs, and so on...
foreach (Doll Child in this.ChildDolls)
{
output += Child.ToString();
}
return output;
} // End of To String method
} // End of class
无论如何。我碰到了一点墙。我需要能够读取并将它们写入 XML 文件(或一些类似的外部文件,我想),因为我的程序最终将涉及其中的 lot;将它们放入代码本身似乎是不明智的。编写应该相对简单,使用类似于示例的 ToString() 方法的技术。但是,由于任意的“深度”,我缺乏事后阅读它们的想法。
【问题讨论】:
-
您能否澄清一下serialize to XML 或其他格式(如 [json](stackoverflow.com/questions/7895105/…) 时遇到的具体问题?不清楚为什么“任意深度”是序列化的问题......
-
因为我不知道序列化是什么-w- XML 对我来说仍然是一个相对较新的工具...