c# – 如何将文本文件大纲列表转换为递归的对象集合?

前端之家收集整理的这篇文章主要介绍了c# – 如何将文本文件大纲列表转换为递归的对象集合?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何将此文本文件内容转换为可以绑定到TreeView的递归对象集合?也就是说,我希望得到一个由3个对象组成的集合,第一个称为拥有三个子对象集合的国家:法国,​​德国,意大利等等……

答:感谢所有帮助过这一点的人,这是我的代码,它成功地将此文本大纲解析为XAML树:http://tanguay.info/web/index.php?pg=codeExamples&id=358

  1. countries
  2. -france
  3. --paris
  4. --bordeaux
  5. -germany
  6. -italy
  7. subjects
  8. -math
  9. --algebra
  10. --calculus
  11. -science
  12. --chemistry
  13. --biology
  14. other
  15. -this
  16. -that

下面的代码是我得到的,但它没有正确处理多个父母的孩子.

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. namespace TestRecursive2342
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. List<OutlineObject> outlineObjects = new List<OutlineObject>();
  12.  
  13. //convert file contents to object collection
  14. List<string> lines = Helpers.GetFileAsLines();
  15. Stack<OutlineObject> stack = new Stack<OutlineObject>();
  16. foreach (var line in lines)
  17. {
  18. OutlineObject oo = new OutlineObject(line);
  19.  
  20. if (stack.Count > 0)
  21. {
  22. OutlineObject topObject = stack.Peek();
  23. if (topObject.Indent < oo.Indent)
  24. {
  25. topObject.OutlineObjects.Add(oo);
  26. stack.Push(oo);
  27. }
  28. else
  29. {
  30. stack.Pop();
  31. stack.Push(oo);
  32. }
  33.  
  34. }
  35. else
  36. {
  37. stack.Push(oo);
  38. }
  39.  
  40. if(oo.Indent == 0)
  41. outlineObjects.Add(oo);
  42. }
  43.  
  44. outlineObjects.ForEach(oo => Console.WriteLine(oo.Line));
  45.  
  46. Console.ReadLine();
  47. }
  48. }
  49.  
  50. public class OutlineObject
  51. {
  52. public List<OutlineObject> OutlineObjects { get; set; }
  53. public string Line { get; set; }
  54. public int Indent { get; set; }
  55.  
  56. public OutlineObject(string rawLine)
  57. {
  58. OutlineObjects = new List<OutlineObject>();
  59. Indent = rawLine.CountPrecedingDashes();
  60. Line = rawLine.Trim(new char[] { '-',' ','\t' });
  61. }
  62. }
  63.  
  64. public static class Helpers
  65. {
  66. public static List<string> GetFileAsLines()
  67. {
  68. return new List<string> {
  69. "countries","-france","--paris","--bordeaux","-germany","-italy","subjects","-math","--algebra","--calculus","-science","--chemistry","--biology","other","-this","-that"};
  70. }
  71.  
  72. public static int CountPrecedingDashes(this string line)
  73. {
  74. int tabs = 0;
  75. StringBuilder sb = new StringBuilder();
  76. foreach (var c in line)
  77. {
  78. if (c == '-')
  79. tabs++;
  80. else
  81. break;
  82. }
  83. return tabs;
  84. }
  85. }
  86. }

解决方法

  1. public class Item
  2. {
  3. public string Name;
  4. public Item Parent;
  5. }
  6.  
  7. List<Item> Collection = new List<Item>();
  8.  
  9. public void Main()
  10. {
  11. var DataSource = data.InnerText;
  12.  
  13. StreamReader Reader = new StreamReader(MapPath("_test2.txt"));
  14. int LastLevel = 0;
  15.  
  16. while (Reader.EndOfStream == false) {
  17. var line = Reader.ReadLine();
  18. var Level = line.Where((System.Object c) => c == "-").Count;
  19. Item LastItem = default(Item);
  20.  
  21. if (Collection.Count != 0) {
  22. LastItem = Collection.Last();
  23. }
  24.  
  25. if (Level == 0) {
  26. Collection.Add(new Item { Name = line });
  27. LastLevel = 0;
  28. }
  29. else if (Level - LastLevel == 1) {
  30. Collection.Add(new Item { Name = line,Parent = LastItem });
  31. LastLevel += 1;
  32. }
  33. else if (Level == LastLevel) {
  34. Collection.Add(new Item { Name = line,Parent = LastItem.Parent });
  35. }
  36. else if (Level < LastLevel) {
  37. var LevelDiff = LastLevel - Level;
  38. Item Parent = LastItem;
  39.  
  40. for (i = 0; i <= LevelDiff; i++) {
  41. Parent = Parent.Parent;
  42. }
  43.  
  44. LastLevel = Level;
  45. Collection.Add(new Item { Name = line,Parent = Parent });
  46. }
  47. }
  48.  
  49. Reader.Close();
  50. }

这应该可以解决问题.我在你的文本文件上测试了它.可能存在一些错误.测试它并告诉它是否有效.

编辑:实际上经过进一步测试后发现这不能按预期工作.您需要添加更多逻辑才能使其正常工作.我把它留给你.

编辑:经过测试代码后,我来到一个更好的版本.我仍然无法保证它在任何情况下都能正常工作.

猜你在找的C#相关文章