The exapmle provided in section “Serializing Derived Classes”(with XmlArrayItemAttribute) may be not the case

cite=https://msdn.microsoft.com/EN-US/library/vstudio/2baksw0z.aspx

Another use of the XmlArrayItemAttribute is to allow the serialization of derived classes. For example, another class named Manager that derives from Employee can be added to the previous example. If you do not apply the XmlArrayItemAttribute, the code will fail at run time because the derived class type will not be recognized. To remedy this, apply the attribute twice, each time setting the Type property for each acceptable type (base and derived).

public class Group{
    [XmlArrayItem(Type = typeof(Employee)),
    XmlArrayItem(Type = typeof(Manager))]
    public Employee[] Employees;
}
public class Employee{
    public string Name;
}
public class Manager:Employee{
    public int Level;
}

A serialized instance might resemble the following.

<Group>
<Employees>
    <Employee>
        <Name>Haley</Name>
    </Employee>
    <Employee xsi:type = "Manager">
        <Name>Ann</Name>
        <Level>3</Level>
    <Employee>
</Employees>
</Group>

However, this is not the case.
I used the following test case:

using System;
using System.IO;
using System.Xml.Serialization;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            var f = new XmlSerializer(typeof (Group));
            using (var sw = new StringWriter())
            {
                f.Serialize(sw, new Group()
                {
                    Employees =
                    new [] {
                        new Employee {Name = "Jerry"},
                        new Manager {Name = "Tom", Level = 20}
                    }
                });
                Console.WriteLine(sw.ToString());
            }
        }
    }

    public class Group
    {
        [XmlArrayItem(Type = typeof (Employee)),
         XmlArrayItem(Type = typeof (Manager))] public Employee[] Employees;
    }

    public class Employee
    {
        public string Name;
    }

    public class Manager : Employee
    {
        public int Level;
    }
}

which yields the result:

<?xml version="1.0" encoding="utf-16"?>
<Group xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Employees>
    <Employee>
      <Name>Jerry</Name>
    </Employee>
    <Manager>
      <Name>Tom</Name>
      <Level>20</Level>
    </Manager>
  </Employees>
</Group>

As you can see, `xsi:type` attribute didn’t appeared in the XML document containing the serialized `Group` instance. Instead, the element name of derived `Manager` class is employed.
If I modify the definition of `Group` class to

    [XmlInclude(typeof(Manager))]
    public class Group
    {
        [XmlArrayItem(Type = typeof (Employee))] public Employee[] Employees;
    }

Then I get the following result

<?xml version="1.0" encoding="utf-16"?>
<Group xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Employees>
    <Employee>
      <Name>Jerry</Name>
    </Employee>
    <Employee xsi:type="Manager">
      <Name>Tom</Name>
      <Level>20</Level>
    </Employee>
  </Employees>
</Group>

which coincides with the example output stated on MSDN.
p.s. I tested the code above in VS 2013.

1 Reply to “The exapmle provided in section “Serializing Derived Classes”(with XmlArrayItemAttribute) may be not the case”

Leave a Reply

Your email address will not be published. Required fields are marked *

ERROR: si-captcha.php plugin: GD image support not detected in PHP!

Contact your web host and ask them to enable GD image support for PHP.

ERROR: si-captcha.php plugin: imagepng function not detected in PHP!

Contact your web host and ask them to enable imagepng for PHP.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Content is available under CC BY-SA 3.0 unless otherwise noted.