public void Read(string text) { Root = new TextLine(); var tempNode = Root; using (TextReader reader = new StringReader(text)) { while (reader.Peek() != -1) { string line = reader.ReadLine(); if (string.IsNullOrEmpty(line)) { continue; } do { TextLine lastChild = tempNode.LastChild; if (lastChild == null) { tempNode.AddChild(line); break; } int curEmptyIndex = GetLeftEmptyCount(line); if (curEmptyIndex == lastChild.LeftEmptyCount) {// 同级 tempNode.AddChild(line); break; } if (curEmptyIndex > lastChild.LeftEmptyCount) { // 子级 lastChild.AddChild(line); tempNode = lastChild; break; } else {// 父级 tempNode = tempNode.parent; } } while (true); } reader.Close(); } }