So, I have recently had occasion to use a technique which, for lack of a better term, I have dubbed a "Matroyshka Class", after the Russian nested dolls. The class has a List property that contains instances of the same class, each of which also has a similar list, to a more-or-less arbitrary "depth".
Here is a simplified example of what that might look like:
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
Anyways. I have run into a bit of a wall. I need the ability to read and write them to an XML file (Or some similar external file, I suppose) because my program will ultimately involve a lot of these; putting them in the code itself seems ill advised. Writing should be relatively straightforward, using a technique similar to the example's ToString() method. However, I'm lacking in ideas for reading them after the fact, because of the arbitrary "depth".