Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  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!");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement