123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Text;
- using System.Xml;
- namespace AIPractice.Shared.Labels
- {
- public class SingleSelectionLabelGroup : LabelGroup
- {
- public SingleSelectionLabelGroup(string id, string title, GroupType type, IEnumerable<ImageLabel> labels) : base(id, title, type, labels)
- {
- }
- public static SingleSelectionLabelGroup FromXml(string xmlContent)
- {
- using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(xmlContent)))
- {
- using (var r = XmlReader.Create(ms))
- {
- r.Read();
- var id = r.GetAttribute("id");
- var title = r.GetAttribute("title");
- var type = (GroupType)int.Parse(r.GetAttribute("type") ?? throw new InvalidOperationException());
- var labels = new List<ImageLabel>();
- while (r.NodeType == XmlNodeType.Element &&
- (r.Name == nameof(DescriptionLabel) ||
- r.Name == nameof(ConclusionLabel)) || r.Read())
- {
- if (r.NodeType == XmlNodeType.EndElement && r.Name == nameof(SingleSelectionLabelGroup))
- {
- break;
- }
- if (r.Name == nameof(DescriptionLabel))
- {
- labels.Add(DescriptionLabel.FromXml(r.ReadOuterXml()));
- }
- if (r.Name == nameof(ConclusionLabel))
- {
- labels.Add(ConclusionLabel.FromXml(r.ReadOuterXml()));
- }
- }
- return new SingleSelectionLabelGroup(id,title, type,labels);
- }
- }
- }
- public override string ToXml()
- {
- var sb = new StringBuilder();
- sb.Append($"<{nameof(SingleSelectionLabelGroup)} id=\"{Id}\" title=\"{Title}\" type=\"{(int)Type}\">");
- foreach (var label in Labels)
- {
- sb.Append(label.ToXml());
- }
- sb.Append($"</{nameof(SingleSelectionLabelGroup)}>");
- return sb.ToString();
- }
- }
- }
|