using System.IO; using System.Text; using System.Xml; namespace AIPractice.Shared.Labels { public class LabelAssociation { /// /// Gets the left label. /// public ImageLabel Left { get; } /// /// Gets the right label. /// public ImageLabel Right { get; } public LabelAssociation(ImageLabel left, ImageLabel right) { Left = left; Right = right; } public static LabelAssociation FromXml(string xmlContent) { ImageLabel left = null; ImageLabel right = null; using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(xmlContent))) { using (XmlReader r = XmlReader.Create(ms)) { r.Read(); while (r.NodeType == XmlNodeType.Element && (r.Name == nameof(DescriptionLabel) || r.Name == nameof(ConclusionLabel)) || r.Read()) { if (r.NodeType == XmlNodeType.EndElement && r.Name == nameof(LabelAssociation)) { break; } if (r.Name == nameof(DescriptionLabel) && left == null) { left = DescriptionLabel.FromXml(r.ReadOuterXml()); } if (r.Name == nameof(DescriptionLabel) && left != null && right == null) { right = DescriptionLabel.FromXml(r.ReadOuterXml()); } if (r.Name == nameof(ConclusionLabel) && left == null) { left = ConclusionLabel.FromXml(r.ReadOuterXml()); } if (r.Name == nameof(ConclusionLabel) && left != null && right == null) { right = ConclusionLabel.FromXml(r.ReadOuterXml()); } } return new LabelAssociation(left, right); } } } public string ToXml() { var sb = new StringBuilder(); sb.Append($"<{nameof(LabelAssociation)}>"); sb.Append(Left.ToXml()); sb.Append(Right.ToXml()); sb.Append($""); return sb.ToString(); } } }