Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Olostan

By: a guest on Sep 14th, 2010  |  syntax: C#  |  size: 1.68 KB  |  views: 48  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.         sealed class MyDumper
  2.         {
  3.             private class Helper
  4.             {
  5.                 public Node Node;
  6.                 public Helper Parent;
  7.                 public string ParentIndent;
  8.                 public bool Last;
  9.             }
  10.             public static string Dump(Node root)
  11.             {
  12.                 var sb = new StringBuilder();
  13.                 var workStack = new Stack<Helper>();
  14.                 var droot = new Helper {Parent = null, Node = root, ParentIndent = ""};
  15.                 workStack.Push(droot);
  16.                 while (workStack.Count > 0)
  17.                 {
  18.                     var current = workStack.Pop();
  19.                     if (current.Parent != null)
  20.                     {
  21.                             sb.Append(current.Parent.ParentIndent);
  22.                             current.ParentIndent = current.Parent.ParentIndent +
  23.                                 (current.Last?" ":"│")+
  24.                                 " ";
  25.                         // my indent
  26.                         sb.Append(current.Last ? "└" : "├");
  27.                         sb.Append("-");
  28.                     }
  29.                     else current.ParentIndent = "";
  30.                     sb.AppendLine(current.Node.Text);
  31.                     for (int index = current.Node.Children.Count-1; index>=0; index--)
  32.                     {
  33.                         var node = current.Node.Children[index];
  34.                         var dnode = new Helper {Node = node, Parent = current, Last = index==current.Node.Children.Count-1};
  35.                         workStack.Push(dnode);
  36.                     }
  37.                 }
  38.                 return sb.ToString();
  39.             }
  40.         }