12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- using System.IO;
- using System.Text;
- using System.Xml;
- namespace AIPractice.Shared.Labels
- {
- public class LabelAssociation
- {
- /// <summary>
- /// Gets the left label.
- /// </summary>
- public ImageLabel Left { get; }
- /// <summary>
- /// Gets the right label.
- /// </summary>
- 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($"</{nameof(LabelAssociation)}>");
- return sb.ToString();
- }
- }
- }
|