2

I'm trying to serialize this class:

[XmlRoot("ArrayOfEvent")]
public class EventList
{
    public EventList()
    {

    }

    public EventList(IEnumerable<Event> items)
    {
        Items = items;
    }

    [XmlArray("")]
    public IEnumerable<Event> Items { get; set; }
}

Here's the Event class contained in EventList.Items:

public class Event
{
    [XmlElement]
    public string ID { get; set; }

    [XmlElement]
    public string Title { get; set; }

    [XmlElement]
    public string Description { get; set; }

    [XmlElement]
    public string Location { get; set; }

    [XmlElement]
    public DateTime? StartTime { get; set; }

    [XmlElement]
    public DateTime? EndTime { get; set; }

    [XmlElement]
    public bool? AllDay { get; set; }
}

And here's where the error occurs:

public static XmlDocument ToXmlDocument<T>(T obj)
{
    var xmlDocument = new XmlDocument();
    var nav = xmlDocument.CreateNavigator();

    if (nav != null)
    {
        using (var writer = nav.AppendChild())
        {
            var ser = new XmlSerializer(typeof(T));
            ser.Serialize(writer, obj); //throws exception
        }
    }

    return xmlDocument;
}

The XmlSerializer gives me this error when calling Serialize():

The type Domain.EventList was not expected. 
Use the XmlInclude or SoapInclude attribute to specify types that are not 
known statically.

I've tried using [XmlInclude(typeof(EventList))] on the EventList class as well as the Event class but neither of those ideas worked. I'm not sure what else to do.

Is anybody familiar with this issue? How can I resolve it? Thanks in advance.

3
  • Perhaps a stupid comment, but you've used [Serializable] attribute? Commented Oct 21, 2013 at 18:39
  • 1
    @gleng XmlSerializer isn't in the least bit interested in [Serializable] Commented Oct 21, 2013 at 18:40
  • @gleng Yes I did try that, but no effect. Commented Oct 21, 2013 at 18:57

1 Answer 1

2

Basically, lists are kinda special to XmlSerializer. You will have a lot more success if you declare the DTO to have a concrete list type, for example:

[XmlArray("")]
public List<Event> Items { get; set; }
Sign up to request clarification or add additional context in comments.

6 Comments

Unfortunately this didn't change anything, either. Same error.
@kehrk firstly, it only recognises (as lists) things that implement IList<T> (or it might be IList plus a non-object indexer - that is a common framework pattern) - but secondly, it only works with concrete list types: if you try it with an interface, it throws an exception saying that it isn't happy with that. Presumably due to reconstruction. There are serializers that don't impose these demands, but this is simply how XmlSerializer works
@kehrk I tried this example - it worked fine for my scenario where that was the list. Can you give a full (runnable) example of it failing?
I added all the relevant code to the OP. Hope it helps. Thanks for looking into it.
@kehrk with the edit: can you show the code that calls ToXmlDocument<T>(T obj) ? I wonder if the thing you are passing as the parameter is not typed as EventList, but is something else - object, ISomeInterface, SomeBaseType, etc
|

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.