简体   繁体   中英

XmlSerializer is not picking up a public array with a XmlArray attribute

I have a container class that I'd like to serialize by calling one of its methods, but when it serializes it doesn't include the public array.

[XmlType("ActivityList")]
public class ActivityList : IEnumerable<Activity>
{
    [XmlArray("Balony")] //The only member I want serialized
    public Activity[] Activities { get; set; }

    public ActivityList()
    {
        this.Activities = new Activity[0];
    }

    public void Save(string filename)
    {
        XmlSerializer serializer = new XmlSerializer(this.GetType());
        using (TextWriter writer = new StreamWriter(filename))
        {
            serializer.Serialize(writer, this);
        }
    }
}

public class Activity
{
    [XmlAttribute]
    public string Name { get; set; }

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

    [XmlIgnore]
    protected Duration Duration { get; private set; }

    public Activity()
    { }
}

我认为您忘记了在[XmlArrayItem("BalonyItem")]下添加[XmlArray("Balony")]

It looks like the XmlSerializer does some funny stuff with objects that inherit from IEnumerable. If you don't inherit from IEnumerable then XmlSerializer finds the array just fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM