Click here to Skip to main content
15,884,629 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi everyone,

I am having a problem in the xml serialization of observable collection.

Here is what I am serializing:
C#
public enum Status { Pending, Active, Completed, Cancelled }

public abstract class Entity : INotifyPropertyChanged
{
    ...
}

public class UserStory : Entity
{
    public uint StoryID { get; set; }
    public Status Status { get; set; }
    ...
    public ObservableCollection<Task> Tasks { get; set; }
}

public class Task : Entity
{
    public uint TaskID { get; set; }
    ...
}


Here is how I serialize it:
C#
public static void SerializeObjectToXML<T>(T item, string FilePath)
{
    XmlSerializer xs = new XmlSerializer(typeof(T));
    using (StreamWriter wr = new StreamWriter(FilePath))
    {
        xs.Serialize(wr, item);
    }
}

public class Main()
{
    ObservableCollection<UserStory> UserStories { get; set; }

    void Main()
    {
        ...
        ObservableCollection<object> Document = new ObservableCollection<object>();
        Document.Add(UserStories);
        SerializeObjectToXML<ObservableCollection<object>>(Document , "...");
        ...
    }
}


But an error occur in the xs.Serialize(wr, item); line saying:

InvalidOperation Exception: There was an error generating the XML document.
Inner Exception: The type ScrumPresentor.ObservableCollection`1[[ScrumPresentor.UserStory, ScrumPresentor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] may not be used in this context. 

I am using Visual Studio 2010, WPF application in .NET 4.0.

Please, I really need your help.
Thanks in advance.
Posted
Updated 25-Dec-11 18:29pm
v5

The XML serializer in .net requires the class you are serializing have a [Serilializable()] attribute.
 
Share this answer
 
Comments
unknowndentified10111 26-Dec-11 0:55am    
ObservableCollection in .NET 4.0 already implements the Serializable attribute. See here for refrence: http://msdn.microsoft.com/en-us/library/ms668604.aspx. I do also try to add the Serializable attribute in all of my entity class but still the error occurs. I am very thankful for your reply, but still It doesn't solve it. Please help.
 
Share this answer
 
v2
Comments
unknowndentified10111 26-Dec-11 4:24am    
Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900