/// <summary> /// Reads the element content as collection. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="reader">The reader.</param> /// <returns></returns> public static List <T> ReadElementContentAsCollection <T>(this XmlReader reader) where T : class { List <T> result = null; var serializer = new XmlSerializer(typeof(T)); var outerXml = reader.ReadOuterXml(); using (var stringReader = new StringReader(outerXml)) { using (var xmlTextReader = XmlTextReaderBuilder.Create(stringReader)) { xmlTextReader.ReadStartElement(); while (!xmlTextReader.EOF) { if (xmlTextReader.NodeType == XmlNodeType.EndElement) { xmlTextReader.ReadEndElement(); continue; } T entity; if (xmlTextReader.IsEmptyElement && xmlTextReader.HasAttributes) { entity = serializer.Deserialize(xmlTextReader) as T; } else { if (xmlTextReader.NodeType != XmlNodeType.Element) { xmlTextReader.Read(); continue; } var subTree = xmlTextReader.ReadSubtree(); entity = serializer.Deserialize(subTree) as T; } if (entity != null) { if (result == null) { result = new List <T>(); } result.Add(entity); } if (!xmlTextReader.IsEmptyElement) { xmlTextReader.Read(); } } } } return(result); }
/// <summary> /// Reads the element content as enumerable. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="reader">The reader.</param> /// <returns></returns> public static IEnumerable <T> ReadElementContentAsEnumerable <T>(this XmlReader reader) where T : class { var serializer = new XmlSerializer(typeof(T)); var outerXml = reader.ReadOuterXml(); using (var stringReader = new StringReader(outerXml)) { using (var xmlTextReader = XmlTextReaderBuilder.Create(stringReader)) { xmlTextReader.ReadStartElement(); while (!xmlTextReader.EOF) { if (xmlTextReader.NodeType == XmlNodeType.EndElement) { xmlTextReader.ReadEndElement(); continue; } T entity; if (xmlTextReader.IsEmptyElement && xmlTextReader.HasAttributes) { entity = serializer.Deserialize(xmlTextReader) as T; } else { var subTree = xmlTextReader.ReadSubtree(); entity = serializer.Deserialize(subTree) as T; } if (entity != null) { yield return(entity); } if (!xmlTextReader.IsEmptyElement) { xmlTextReader.Read(); } } } } }
/// <summary> /// Deserializes the XML document contained by the specific System.String. /// </summary> /// <typeparam name="TOut">The type of objects to deserialize.</typeparam> /// <param name="xml">The System.String that contains the XML document to deserialize.</param> /// <returns> /// The T object being deserialized. /// </returns> /// <exception cref="System.InvalidOperationException"> /// An error occurred during deserialization. The original exception is available /// using the System.Exception.InnerException property. /// </exception> // ReSharper disable once InconsistentNaming private static TOut XmlDeserializeEntity <TOut>(string xml) where TOut : new() { if (xml.IsNullOrWhiteSpace()) { throw new ArgumentNullException(nameof(xml), $"Could not deserialize null or empty input for type '{typeof(TOut).Name}'."); } using (var textReader = new StringReader(xml)) { using (var xmlReader = XmlTextReaderBuilder.Create(textReader)) { var serializer = new XmlSerializer(typeof(TOut)); var entity = serializer.Deserialize(xmlReader); if (entity is TOut t) { return(t); } return(default);