0

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.

8
  • To get a node per element; use [XmlElement] instead of [XmlArray] Commented Apr 2, 2018 at 20:53
  • The Net method for serializing expects a single note at the root. the is why having an array of Person is giving an error. Having a single Person element at the root does not give an error. Commented Apr 2, 2018 at 21:02
  • @MarcGravell yeah I've gotten that far with using [XmlElement] but I can't add child classes to the List without getting the error. Thanks Commented Apr 2, 2018 at 21:26
  • @jdweng The Person element isn't at the root, I just didn't include the whole structure. My conflict is with the fact that my List<base class> which holds derived child elements, fails at runtime due it saying it doesn't know about the child classes. Nothing I've tried rectifies that. Thanks Commented Apr 2, 2018 at 21:26
  • It looks like you may be trying to deserialize a to a polymorphic type hierarchy where Person has subclasses Child and Adult. 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 your Person, Child and Adult types as well as the complete required XML. Commented Apr 2, 2018 at 22:18

1 Answer 1

0

Try following :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            Root root = new Root() {
                PersonList = new List<Person>() {
                    new Person() {
                        person = "Person1",
                        child = "Child1"
                    },
                    new Person() {
                        person = "Person2",
                        adult = "Adult2"
                    }
                }
            };

            XmlSerializer serializer = new XmlSerializer(typeof(Root));
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            XmlWriter writer = XmlWriter.Create(FILENAME,settings);
            serializer.Serialize(writer, root);
        }
    }
    public class Root
    {
        [XmlElement("Person")]
        public List<Person> PersonList { get; set; }

    }
    public class Person
    {
        [XmlElement("Person")]
        public string person { get; set; }

        [XmlElement("Child")]
        public string child { get; set; }

        [XmlElement("Adult")]
        public string adult { get; set; }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.