static void Main(string[] args) { var dictionary = new Dictionary <int, Node>(); using (TextReader reader = File.OpenText("test.txt")) { string line; while ((line = reader.ReadLine()) != null) { RawNode raw = RawNode.FromLine(line); Node parent = raw.ParentId == 0 ? null : dictionary[raw.ParentId]; Node node = new Node { Id = raw.Id, Parent = parent, Title = raw.Title }; if (parent != null) { parent.Children.Add(node); } dictionary[node.Id] = node; } } Node root = dictionary[1]; root.Dump(); }