using System.Collections.Generic;
using System.Xml.Serialization;
using System.Diagnostics;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Text.RegularExpressions;
using System.Globalization;
using System.ComponentModel.DataAnnotations;
using System.Collections;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Threading.Tasks;
public abstract class EntityBase
public string AttributeValue { get; set; }
public string ElementValue { get; set; }
public class Entity : EntityBase
public class UndesiredEntity : EntityBase
public RootObject() { this.Entities = new List<EntityBase>(); }
[XmlElement("Entity", typeof(Entity))]
[XmlElement("UndesiredEntity", typeof(UndesiredEntity))]
public List<EntityBase> Entities { get; set; }
public class TAxEntity : Entity
string EntityElementName { get { return "TAxEntity"; } }
private Task<List<TAxEntity>> Deserialize(XmlReader reader)
var entities = reader.DeserializeSequence<TAxEntity>(EntityElementName, "" ).ToList();
return Task.FromResult(entities);
public static void Test()
public static void Test(string attributeValue, string elementValue)
var test = new RootObject
new Entity { AttributeValue = attributeValue, ElementValue = elementValue },
new Entity { AttributeValue = attributeValue, ElementValue = elementValue },
new Entity { AttributeValue = attributeValue, ElementValue = elementValue },
new Entity { AttributeValue = attributeValue, ElementValue = elementValue },
new Entity { AttributeValue = attributeValue, ElementValue = elementValue },
new UndesiredEntity { AttributeValue = attributeValue, ElementValue = elementValue },
new Entity { AttributeValue = attributeValue, ElementValue = elementValue },
new Entity { AttributeValue = attributeValue, ElementValue = elementValue },
new UndesiredEntity { AttributeValue = attributeValue, ElementValue = elementValue },
new Entity { AttributeValue = attributeValue, ElementValue = elementValue },
DoTest(test, new XmlWriterSettings { Indent = false });
DoTest(test, new XmlWriterSettings { Indent = true });
static void DoTest(RootObject root, XmlWriterSettings settings)
var xml = GetXml(root, settings, null, null);
Console.WriteLine("Testing sequential deserialization of entity nodes from XML: ");
using (var reader = XmlReader.Create(new StringReader(xml)))
reader.ReadToDescendant("Entity", "");
list = reader.DeserializeSequence<Entity>("Entity", "").ToList();
var count1 = root.Entities.OfType<Entity>().Count();
var count2 = root.Entities.OfType<Entity>().Count();
Assert.IsTrue(count1 == count2);
Console.WriteLine("Count of {0} Entity nodes is correct after deserialization.\n", count1);
public static string GetXml<T>(T obj, XmlWriterSettings settings, XmlSerializer serializer, XmlSerializerNamespaces ns)
serializer = serializer ?? new XmlSerializer(obj.GetType());
using (var textWriter = new StringWriter())
using (var xmlWriter = XmlWriter.Create(textWriter, settings))
serializer.Serialize(xmlWriter, obj, ns);
return textWriter.ToString();
public static class XmlReaderExtensions
public static IEnumerable<TElement> DeserializeSequence<TElement>(this XmlReader reader, string localEntityElementName, string namespaceURI)
throw new ArgumentNullException();
var deserializer = new XmlSerializer(typeof(TElement));
while ((reader.NodeType == XmlNodeType.Element && reader.LocalName == localEntityElementName && reader.NamespaceURI == namespaceURI)
|| reader.ReadToNextSibling(localEntityElementName, namespaceURI))
using (var subReader = reader.ReadSubtree())
element = (TElement)deserializer.Deserialize(subReader);
public class AssertionFailedException : System.Exception
public AssertionFailedException() : base() { }
public AssertionFailedException(string s) : base(s) { }
public static class Assert
public static void IsTrue(bool value)
throw new AssertionFailedException("failed");
public static void Main()