在C#中用XML文件创建一个括号树

时间:2016-12-04 03:49:53

标签: c# xml

我正在尝试从XML文件为括号创建树。

括号中的匹配由四个小组组成。该团队的获胜者将进入下一轮。

如果一场比赛由3支球队和一场儿童比赛组成,那么子比赛将确定第四支球队的比赛。

这是我的XML文件:

var add = ({a, b}) => ({sum: a + b})

这是我创建树的功能:

<?xml version="1.0" encoding="utf-8" ?>
  <Round>
    <Match>
      <Team id="A1">Apples</Team>
      <Team id="B2">Banannas</Team>
      <Team id="C3">Cantalopes</Team>
      <Team id="D4">Durians</Team>
    </Match>
    <Match>
      <Team id="A2">Avocados</Team>
      <Team id="B2">Blueberry</Team>
      <Team id="C3">Coconut</Team>
      <Team id="TBD">
        <Match>
          <Team id="A3">Apricots</Team>
          <Team id="B5">Banannas</Team>
          <Team id="C9">Cantalopes</Team>
          <Team id="D6">Durians</Team>
        </Match>
      </Team>
    </Match>
  </Round>

我最初通过以下方式调用此函数:

   private void CreateTree(XmlTextReader reader, Node<Match<Team>> parent,  Match<Team> currentMatch)
    {
        if (reader.Read())
        {
            if (reader.IsStartElement())
            {
                switch (reader.Name)
                {
                    case "Match": //We need to make a new match!
                        currentMatch = new Match<Team>();
                        Node<Match<Team>> child = new Node<Match<Team>>(currentMatch);
                        parent.Add(currentMatch);
                        CreateTree(reader, child, currentMatch);
                        break;
                    case "Team": //We need to add a team!
                        string id = reader.GetAttribute("id");
                        if (id != "TBD")
                        {
                            string name = reader.Value;
                            currentMatch.Add(new Team(id, name));
                        }
                        else
                        {
                            currentMatch.Add(Team.Tbd);
                        }
                        CreateTree(reader, parent, currentMatch);
                        break;
                    default:
                        break;
                }
            }
            else
            {
              CreateTree(reader, parent.Parent, null);
            }
        }
    }

不幸的是,这不起作用,我的树是空的,我无法弄清楚我做错了什么。任何帮助搞清楚这一点将不胜感激!

谢谢!

0 个答案:

没有答案