1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Text;
- using System.Xml;
- namespace AIPractice.Shared.Labels
- {
- /// <summary>
- /// All labels in this group can be multi-selected.
- /// </summary>
- public class MultiSelectionLabelGroup:LabelGroup
- {
- public MultiSelectionLabelGroup(string id, string title, GroupType type, IEnumerable<ImageLabel> labels) : base(
- id, title, type, labels)
- {
- }
- public static MultiSelectionLabelGroup 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(MultiSelectionLabelGroup))
- {
- 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 MultiSelectionLabelGroup(id, title, type, labels);
- }
- }
- }
- public override string ToXml()
- {
- var sb = new StringBuilder();
- sb.Append($"<{nameof(MultiSelectionLabelGroup)} id=\"{Id}\" title=\"{Title}\" type=\"{(int)Type}\">");
- foreach (var label in Labels)
- {
- sb.Append(label.ToXml());
- }
- sb.Append($"</{nameof(MultiSelectionLabelGroup)}>");
- return sb.ToString();
- }
- }
- }
|