You create a class library that contains the class hierarchy defined in the following code segment.
(Line numbers are included for reference only.)
01 public class Group {
02 public Employee[ ] Employees;
03 }
04 public class Employee {
05 public string Name;
06 }
07 public class Manager : Employee {
08 public int Level;
09 }
You create an instance of the Group class. You populate the fields of the instance.
When you attempt to serialize the instance by using the Serialize method of the XmlSerializer class, you receive InvalidOperationException.
You also receive the following error message: “There was an error generating the XML document.”
You need to modify the code segment so that you can successfully serialize instances of the Group class by using the XmlSerializer class.
You also need to ensure that the XML output contains an element for all public fields in the class hierarchy.
What should you do?
A.
Insert the following code between lines 1 and 2 of the code segment:
[XmlArrayItem(Type = typeof(Employee)),
XmlArrayItem(Type = typeof(Manager))]
B.
Insert the following code between lines 1 and 2 of the code segment:
[XmlElement(Type = typeof(Employees))]
C.
Insert the following code between lines 1 and 2 of the code segment:
[XmlArray(ElementName=”Employees”)]
D.
Insert the following code between lines 3 and 4 of the code segment:
[XmlElement(Type = typeof(Employee))]
And
Insert the following code between lines 6 and 7 of the code segment:
[XmlElement(Type = typeof(Manager))]
I agree with the answer. A