SHARE
TWEET

Untitled

a guest Oct 16th, 2019 80 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var hierarchy = new List<Hierarchy>();
  2.             var lines = File.ReadAllLines("taxonomy.txt");
  3.  
  4.             foreach (var line in lines)
  5.             {
  6.                 var splitLine = line.Split(" > ").ToList();
  7.  
  8.                 if (splitLine.Count == 1)
  9.                     hierarchy.Add(new Hierarchy { Name = splitLine[0], Children = new List<Hierarchy>() });
  10.  
  11.                 var newHierarchy = hierarchy.Find(x => x.Name == splitLine[0]);
  12.                 for(int i = 1; i < splitLine.Count; i++)
  13.                 {
  14.                     if (i == splitLine.Count - 1)
  15.                         newHierarchy.Children.Add(new Hierarchy { Name = splitLine[i], Children = new List<Hierarchy>() });
  16.                     else
  17.                         newHierarchy = newHierarchy.Children.First(x => x.Name == splitLine[i]);
  18.                 }
  19.             }
  20.  
  21.             File.WriteAllLines("output.txt", hierarchy.Select(x => JsonConvert.SerializeObject(x)));
  22.  
  23.             Console.WriteLine("Hello World!");
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top