在main static void上显示类列表

时间:2016-11-23 22:18:46

标签: c#

我希望输出我已经制作的列表,通常我只为每个循环执行一次操作并调出列表,如下面的代码所示。控制台要我纠正它,但它没有显示,因为引用为空。 谢谢你的帮助

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>();
}

下面是.xml的示例,但我知道这不是问题

&#13;
&#13;
<?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>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

我不知道我是否帮助你。但我希望这可能会有所帮助:

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);
    }
}

您在树中迭代您的家人,然后递归打印人(及其子女)。你应该看到类似的东西:

Families

希望这有帮助!

答案 1 :(得分:0)

所以你必须像下面一样遍历你的人名单。

 foreach (Family per in tree.families) // <--- THE ERROR IS HERE
 {
            Console.WriteLine(per.FamilyName + " " + per.FamilyTotalReign);
            foreach (var ppl in per.People)
            {
                Console.WriteLine(ppl.PersonName + " " + per.FamilyName);
            }
 }

但我建议你使用XmlSerializer,你可以将你的XML反序列化为object.This更干净的方法。如果你想要示例代码,请告诉我。