1
0

MultiSelectionLabelGroup.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. using System.Xml;
  6. namespace AIPractice.Shared.Labels
  7. {
  8. /// <summary>
  9. /// All labels in this group can be multi-selected.
  10. /// </summary>
  11. public class MultiSelectionLabelGroup:LabelGroup
  12. {
  13. public MultiSelectionLabelGroup(string id, string title, GroupType type, IEnumerable<ImageLabel> labels) : base(
  14. id, title, type, labels)
  15. {
  16. }
  17. public static MultiSelectionLabelGroup FromXml(string xmlContent)
  18. {
  19. using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(xmlContent)))
  20. {
  21. using (var r = XmlReader.Create(ms))
  22. {
  23. r.Read();
  24. var id = r.GetAttribute("id");
  25. var title = r.GetAttribute("title");
  26. var type = (GroupType) int.Parse(r.GetAttribute("type") ?? throw new InvalidOperationException());
  27. var labels = new List<ImageLabel>();
  28. while (r.NodeType == XmlNodeType.Element &&
  29. (r.Name == nameof(DescriptionLabel) ||
  30. r.Name == nameof(ConclusionLabel)) || r.Read())
  31. {
  32. if (r.NodeType == XmlNodeType.EndElement && r.Name == nameof(MultiSelectionLabelGroup))
  33. {
  34. break;
  35. }
  36. if (r.Name == nameof(DescriptionLabel))
  37. {
  38. labels.Add(DescriptionLabel.FromXml(r.ReadOuterXml()));
  39. }
  40. if (r.Name == nameof(ConclusionLabel))
  41. {
  42. labels.Add(ConclusionLabel.FromXml(r.ReadOuterXml()));
  43. }
  44. }
  45. return new MultiSelectionLabelGroup(id, title, type, labels);
  46. }
  47. }
  48. }
  49. public override string ToXml()
  50. {
  51. var sb = new StringBuilder();
  52. sb.Append($"<{nameof(MultiSelectionLabelGroup)} id=\"{Id}\" title=\"{Title}\" type=\"{(int)Type}\">");
  53. foreach (var label in Labels)
  54. {
  55. sb.Append(label.ToXml());
  56. }
  57. sb.Append($"</{nameof(MultiSelectionLabelGroup)}>");
  58. return sb.ToString();
  59. }
  60. }
  61. }