SingleSelectionLabelGroup.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. public class SingleSelectionLabelGroup : LabelGroup
  9. {
  10. public SingleSelectionLabelGroup(string id, string title, GroupType type, IEnumerable<ImageLabel> labels) : base(id, title, type, labels)
  11. {
  12. }
  13. public static SingleSelectionLabelGroup FromXml(string xmlContent)
  14. {
  15. using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(xmlContent)))
  16. {
  17. using (var r = XmlReader.Create(ms))
  18. {
  19. r.Read();
  20. var id = r.GetAttribute("id");
  21. var title = r.GetAttribute("title");
  22. var type = (GroupType)int.Parse(r.GetAttribute("type") ?? throw new InvalidOperationException());
  23. var labels = new List<ImageLabel>();
  24. while (r.NodeType == XmlNodeType.Element &&
  25. (r.Name == nameof(DescriptionLabel) ||
  26. r.Name == nameof(ConclusionLabel)) || r.Read())
  27. {
  28. if (r.NodeType == XmlNodeType.EndElement && r.Name == nameof(SingleSelectionLabelGroup))
  29. {
  30. break;
  31. }
  32. if (r.Name == nameof(DescriptionLabel))
  33. {
  34. labels.Add(DescriptionLabel.FromXml(r.ReadOuterXml()));
  35. }
  36. if (r.Name == nameof(ConclusionLabel))
  37. {
  38. labels.Add(ConclusionLabel.FromXml(r.ReadOuterXml()));
  39. }
  40. }
  41. return new SingleSelectionLabelGroup(id,title, type,labels);
  42. }
  43. }
  44. }
  45. public override string ToXml()
  46. {
  47. var sb = new StringBuilder();
  48. sb.Append($"<{nameof(SingleSelectionLabelGroup)} id=\"{Id}\" title=\"{Title}\" type=\"{(int)Type}\">");
  49. foreach (var label in Labels)
  50. {
  51. sb.Append(label.ToXml());
  52. }
  53. sb.Append($"</{nameof(SingleSelectionLabelGroup)}>");
  54. return sb.ToString();
  55. }
  56. }
  57. }