How to deserialize an XML array containing multiple types of elements in C#

There are two ways to do this; the first is to do something like:

[XmlArray("players")]
[XmlArrayItem("skater", Type=typeof(Skater))]
[XmlArrayItem("goalie", Type=typeof(Goalie))]
public List<SomeCommonBaseClass> Players { get; set; }

which maps the two element types inside a single collection. Worst case, SomeCommonBaseClass could be object:

[XmlArray("players")]
[XmlArrayItem("skater", Type=typeof(Skater))]
[XmlArrayItem("goalie", Type=typeof(Goalie))]
public List<object> Players { get; set; }

The second is to make <players> map to a wrapper object:

[XmlElement("players")]
public Players Players { get;set;}
...
public class Players
{
    [XmlElement("skater")]
    public List<Skater> Skaters {get;set;}

    [XmlElement("goalie")]
    public List<Goalie> Goalies {get;set;}
}

Which to choose depends on the circumstance; the latter allows things like "at most one goalie", by changing it to:

    [XmlElement("goalie")]
    public Goalie Goalie {get;set;}

Comments

  1. Alessandro

    • 2019/7/22

    which maps the two element types inside a single collection. Worst case, SomeCommonBaseClass could be object : [XmlArray("players")] [XmlArrayItem("skater", 

  2. Braden

    • 2017/4/5

    How to deserialize an XML array containing multiple types of elements in C# League object containing a Skaters list with 3 elements and a Goalies list with 2

  3. Parker

    • 2020/7/19

    I'm trying to use XMLSerializer to generate XML such as the following, where the contents of <create> is an array, but the elements can be of differing 

  4. Kairo

    • 2019/1/30

    string serializedListOfCustomers = File.ReadAllText("c:\\temp\\serialized.txt", Encoding.UTF8); List<MyCustomer> custList = XmlCamelOverrides.Deserialize<List<MyCustomer>>(serializedListOfCustomers); Console.WriteLine("custList has {0} elements.", custList.Count); }

  5. Beau

    • 2019/6/11

    It will help you with serializing objects into XML in one line of code. array /// of additional object types to deserialize.

  6. Kayden

    • 2019/2/5

    var xmlStr = File.ReadAllText(file); // get text from xml file; var ms = new MemoryStream(Encoding.UTF8.GetBytes(xmlStr)); // get bytes from that text; XmlSerializer serializer = new XmlSerializer(typeof (Note)); // Initialize de serializer with the "Note" Type; Note msn = ((Note)serializer.Deserialize(ms)); // call deserialization (Object result)

  7. Lopez

    • 2021/4/25

    Namespace) during deserialization, the array contains any XML elements that are complex types in XML are bound to C structs or C++ classes, and so on.

  8. Ahmed

    • 2018/12/5

    XmlSerializer serializer = new XmlSerializer (typeof(OrderedItem)); // Create a TextReader to read the file. FileStream fs = new FileStream (filename, FileMode.OpenOrCreate); TextReader reader = new StreamReader (fs); // Declare an object variable of the type to be deserialized.

  9. Kaleb

    • 2019/3/12

    Gets or sets the XML data type of the generated XML element. Several instances of the XmlArrayItemAttribute class are applied to three members, 

  10. Ezequiel

    • 2016/3/2

    public class HouseHold : IXmlSerializable { private static Type[] _animalTypes; static HouseHold() { _animalTypes = GetAnimalTypes().ToArray(); } public HouseHold() { this.Residents = new List<Animal>(); } public List<Animal> Residents { get; set; } #region IXmlSerializable Members public System.Xml.Schema.XmlSchema GetSchema() { return null; } public void ReadXml(System.Xml.XmlReader reader) { bool wasEmpty = reader.IsEmptyElement; reader.Read(); if (wasEmpty) return; reader.MoveToContent

  11. Judson

    • 2017/1/16

    Deserialize(fs); // Read the element names and values. foreach(XmlElement xel in If you specify a Name value, the array contains only XML elements with 

  12. Koroveshi

    • 2016/5/17

    I am not using schema to validate my XML. However by doing reverse engineering and generating schema from my XML, it looks as below; Also I am not using .Net 1.1, I am using .Net 3.5. So let me know what is the replacement for the ArrayList and how I can address the problem stated above. Schema:

  13. Caiden

    • 2017/3/3

    The xsi:type attributes you are seeing in your XML are standard W3C XML Schema attributes that allow an element to explicitly specify its type; for details see 

  14. Kyro

    • 2017/11/5

    XmlSerializer deserializer = new XmlSerializer(typeof (AddressDirectory)); TextReader reader = new StreamReader(@" D:\myXml.xml"); object obj = deserializer.Deserialize(reader); AddressDirectory XmlData = (AddressDirectory)obj; reader.Close(); The resultant object "XmlData" will contain a list of object of type "Address".

  15. King

    • 2016/8/20

    If the object of this class were a property, then it would be marked, for example, with XmlElement attribute. But it's not an element. Here, 

  16. Azrael

    • 2017/6/23

    to serialize and deserialize XML that contains an element that is a different method: by using the Type value of the XmlArrayItem 

  17. Ferretti

    • 2015/2/16

    Namespace), during deserialization, this array will contain any unknown XML elements that are not already contained in the other arrays. If a class contains 

Comments are closed.

Recent Posts