Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# .NET4
Hi everyone,
 
I am having a problem in the xml serialization of observable collection.
 
Here is what I am serializing:
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:
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 25-Dec-11 17:51pm
Edited 25-Dec-11 18:29pm
v5
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

The XML serializer in .net requires the class you are serializing have a [Serilializable()] attribute.
  Permalink  
Comments
unknowndentified10111 at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

  Permalink  
v2
Comments
unknowndentified10111 at 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)



Advertise | Privacy | Mobile
Web01 | 2.8.141015.1 | Last Updated 26 Dec 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100