Kaan Kaan - 11 days ago 5
C# Question

Displaying a class list on the main static void

I'm looking to output the list that I've made and usually I just do a for each loop and call up the list like shown in the code below. The console wants me to correct it but then it doesn't display as the reference is null.
Thanks for any help

class Program
{
const string FILENAME = @"royalTreeResults.xml";
// THIS SECTION OF CODE IS WHAT IT SUGGESTS private static readonly IEnumerable<Family> families;

static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
Tree tree = new Tree();
tree.families = doc.Descendants("family").Select(x => new Family()
{
FamilyName = (string)x.Element("name"),
FamilyTotalReign = (int)x.Element("totalReign"),
People = x.Elements("person").Select(y => Person.Recursive(y)).ToList()
}).ToList();
foreach(Family per in families) // <--- THE ERROR IS HERE
{
Console.WriteLine(per.FamilyName + " " + per.FamilyTotalReign + " " + per.People);
}
}
}
public class Family
{
public string FamilyName { get; set; }
public int FamilyTotalReign { get; set; }
public List<Person> People { get; set; }
}
public class Person
{
public int? PersonBorn { get; set; }
public int? PersonCoronation { get; set; }
public int? PersonDied { get; set; }
public string PersonName { get; set; }
public int? PersonYinPower { get; set; }
public List<Person> Children { get; set; }

public static Person Recursive(XElement person)
{
Person newPerson = new Person();
newPerson.PersonName = (string)person.Element("name");
newPerson.PersonYinPower = (int?)person.Element("yearsInpower");
newPerson.PersonBorn = (int?)person.Element("born");
newPerson.PersonDied = (int?)person.Element("died");
newPerson.PersonCoronation = (int?)person.Element("coronation");
if (person.Element("children") != null)
{
newPerson.Children = person.Element("children").Elements("person").Select(y => Person.Recursive(y)).ToList();
}
return newPerson;
}
}
public class Tree
{
public List<Family> families = new List<Family>();
}


A sample of the .xml is below, however I know this is not the problem



<?xml version="1.0" encoding="utf-8"?>
<royaltree>
<family>
<name>Wessex</name>
<totalReign>137</totalReign>
<person>
<name>Alfred the Great</name>
<yearsInpower>28</yearsInpower>
<born>849</born>
<died>899</died>
<coronation>871</coronation>
<children>
<person>
<name>Edward the Elder</name>
<yearsInpower>25</yearsInpower>
<born>879</born>
<died>924</died>
<coronation>899</coronation>
<children>
<person>
<name>Edmund I the Elder</name>
<yearsInpower>6</yearsInpower>
<born>939</born>
<died>946</died>
<coronation>940</coronation>
<children>
<person>
<name>Edger the Peaceful</name>
<yearsInpower>16</yearsInpower>
<born>944</born>
<died>975</died>
<coronation>959</coronation>
<children>
<person>
<name>Ethelred II</name>
<yearsInpower>38</yearsInpower>
<born>962</born>
<died>1016</died>
<coronation>978</coronation>
<children>
<person>
<name>Edward Confessor</name>
<yearsInpower>24</yearsInpower>
<born>1002</born>
<died>1066</died>
<coronation>1042</coronation>
</person>
<person>
<name>Edward II Ironside</name>
<yearsInpower>0</yearsInpower>
<born>1002</born>
<died>1016</died>
<coronation>1016</coronation>
</person>
</children>
</person>
</children>
</person>
</children>
</person>
</children>
</person>
</children>
</person>
</family>
<family>
<name>Norman</name>
<totalReign>69</totalReign>
<person>
<name>William I</name>
<yearsInpower>21</yearsInpower>
<born>1028</born>
<died>1087</died>
<coronation>1066</coronation>
<children>
<person>
<name>Adela</name>
<born>1050</born>
<died>1080</died>
</person>
<person>
<name>William II</name>
<yearsInpower>13</yearsInpower>
<born>1056</born>
<died>1100</died>
<coronation>1087</coronation>
</person>
<person>
<name>Henry I Beauclerc</name>
<yearsInpower>35</yearsInpower>
<born>1068</born>
<died>1135</died>
<coronation>1100</coronation>
<children>
<person>
<name>Matilda</name>
<born>1130</born>
<died>1167</died>
</person>
</children>
</person>
</children>
</person>
</family>




Answer

I don't know if I get you right. But I hope this might be helpful:

static void Main(string[] args)
{
    //... your stuff

    //Iterate through families in tree
    foreach (Family per in tree.families)
    {
        //Show the family info
        Console.WriteLine(per.FamilyName + " " + per.FamilyTotalReign);

        //Show the members recursively
        ShowPeopleRecursive(per.People, 1);
    }
}

//Shows the people tree.
private static void ShowPeopleRecursive(List<Person> people, int level)
{
    const string indent = "";
    foreach (var p in people)
    {
        Console.WriteLine($"{indent.PadLeft(level, ' ')} {p.PersonName} ({p.PersonBorn} - {p.PersonDied}). Years in power: {p.PersonYinPower}");

        if (p.Children != null)
            ShowPeopleRecursive(p.Children, level + 1);
    }
}

You iterate over your families in the tree and then recursively print the people (and their children). You should see something like:

Families

Hope this helps!

Comments