I'm trying to serialize a List which can hold objects of the child classes. I need it to lack any parent tag, so XMLElement on the List has helped with that. My problem is with the reflection of the List not knowing what the child tags are.
I have found that my issue comes in when I add the [XmlRoot] tag to the class that represents my root element.
[XmlRoot("EFilingBatchXML", Namespace = "www.fincen.gov/base", IsNullable = false)]
public class XmlRoot
Before that the List works and I can use either [XmlInclude] for the child classes, or include a Type[] when making the XmlSerializer. There is the small issue of it including the attribute:
xsi:type="Child"
xsi:type="Adult"
So it looks like the Root tag is causing my problem, but I will need to know how to exclude those attributes as well.
Ex)
[XMLElement("Person")]
public List<Person> PersonList { get; set; }
.
.
PersonList = new List<Person>() {
new Child(), new Adult()
};
I need it to format as such
<root>
...
<Person>
<Person elements>
<Child elements>
</Person>
<Person>
<Person elements>
<Adult elements>
</Person>
...
</root>
I always get the error
Child was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically.
Any help would be appreciated as I'm running out of ideas, and have no idea why the Type[] solution doesn't work for this.
[XmlElement]instead of[XmlArray]Personhas subclassesChildandAdult. If that's correct, this would seem to be a duplicate of Using XmlSerializer to serialize derived classes. As shown there, you would apply[XmlElement("Child", Type = typeof(Child))]to your list. But if that doesn't work for you we would need to see a minimal reproducible example showing yourPerson,ChildandAdulttypes as well as the complete required XML.