/// <summary> /// Validates the specified message content. /// Ensures that the <see cref="!:messageContent"/> contains all manadtory XML elements. /// </summary> /// <param name="messageContent">Content of the message.</param> /// <exception cref="ArgumentException">Value cannot be null or whitespace. - messageContent</exception> /// <exception cref="XmlContentParserException"></exception> public void Validate(string messageContent) { if (string.IsNullOrWhiteSpace(messageContent)) { throw new ArgumentException("Value cannot be null or whitespace.", nameof(messageContent)); } IEnumerable <string> mandatoryXmlElements = _validationRepository.GetMandatoryXmlElements().ToList(); bool isValid = mandatoryXmlElements.All(xmlElement => { string xmlContent = _identifyXmlElementsService.ExtractXmlContent(messageContent, RegularExpressions.XmlContentRegex, xmlElement); return(!string.IsNullOrWhiteSpace(xmlContent) && !xmlContent.Equals($"{xmlElement}{xmlElement.Insert(1, "/")}")); }); if (!isValid) { throw new XmlContentParserException($"The specified message content does not contain all mandatory XML elements. Mandatory elements: '{string.Join(",", mandatoryXmlElements)}'."); } }
private T ExtractAndDeserializeXmlElement <T>(string messageContent, string element) { string xmlContent = _identifyXmlElementsService.ExtractXmlContent(messageContent, RegularExpressions.XmlContentRegex, element); return(!string.IsNullOrWhiteSpace(xmlContent) ? _xmlDeserializerService.Deserialize <T>(xmlContent) : default(T)); }