private static ToplistBlock ConvertXElementToBlock(XElement xmlBlock) { xmlBlock.ThrowIfNull(nameof(xmlBlock)); string title = XDocumentParser.GetAttributeValue(xmlBlock, nameof(ToplistBlock.Title)); int number = XDocumentParser.GetAttributeValue <int>(xmlBlock, nameof(ToplistBlock.Number)); XElement?itemsXml = XDocumentParser.FindSubelement(xmlBlock, nameof(ToplistBlock.Items)); if (itemsXml is null) { throw new ArgumentException( "Invalid strcuture of XML document: cannot find toplist items block.", nameof(xmlBlock) ); } var result = new ToplistBlock(title, number); result.UpdateItems( XDocumentParser.FindSubelements(itemsXml) .Select(xmlItem => ConvertXElementToItem(xmlItem, result)) .ToList() ); return(result); }
public override bool AddBlock(ToplistBlock block) { block.ThrowIfNull(nameof(block)); int insertIndex = CalculateInsertIndex(block.Number); Blocks.Insert(insertIndex, block); return(true); }
private static ToplistItem ConvertXElementToItem(XElement xmlItem, ToplistBlock parentBlock) { xmlItem.ThrowIfNull(nameof(xmlItem)); string name = XDocumentParser.GetAttributeValue(xmlItem, nameof(ToplistItem.Name)); int parsedPosition = XDocumentParser.GetAttributeValue <int>( xmlItem, nameof(ToplistItem.Position) ); int?position = parsedPosition == -1 ? (int?)null : parsedPosition; return(new ToplistItem(name, position, parentBlock)); }
private static XElement ConvertBlockToXElement(ToplistBlock block) { block.ThrowIfNull(nameof(block)); return(new XElement("Block", new XAttribute(nameof(ToplistBlock.Title), block.Title), new XAttribute(nameof(ToplistBlock.Number), block.Number), new XElement(nameof(block.Items), block.Items.Select(ConvertItemToXElement).ToArray() ) )); }
public override bool RemoveBlock(ToplistBlock block) { block.ThrowIfNull(nameof(block)); return(Blocks.Remove(block)); }
public void RemoveBlock(ToplistBlock block) { block.ThrowIfNull(nameof(block)); _toplist.RemoveBlock(block); }