Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6984741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:35:19+00:00 2026-05-27T18:35:19+00:00

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

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:35:19+00:00Added an answer on May 27, 2026 at 6:35 pm

    Try using the System.Xml.Serialization.XmlInclude attribute. I’m not sure if I correctly understand your intent of a collection of collections, but assuming that is what you want, here is a working solution. The output follows the code:

    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.IO;
    using System.Xml.Serialization;
    
    namespace StackoverflowXxmlSerialize
    {
    
        public enum Status { Pending, Active, Completed, Cancelled }
    
        [System.Xml.Serialization.XmlInclude(typeof(UserStory))]
        [System.Xml.Serialization.XmlInclude(typeof(Task))]    
        public abstract class Entity : INotifyPropertyChanged 
        {
            public event PropertyChangedEventHandler PropertyChanged;
        }
    
    
        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; }
        }
    
        class Util
        {
            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 TestSerialize
        {
            static ObservableCollection<Entity> UserStories { get; set; }
    
            public static void RunTest()
            {
                UserStories = new ObservableCollection<Entity> { 
                    new UserStory  {
                    StoryID = 127,
                    Status = Status.Active,
                    Tasks = new ObservableCollection<Task>{new Task { TaskID = 11 }, new Task { TaskID = 12 }}
                    },
    
                    new UserStory  {
                    StoryID = 128,
                    Status = Status.Cancelled,
                    Tasks = new ObservableCollection<Task>{new Task { TaskID = 13 }, new Task { TaskID = 14 }}
                    },
    
                    new UserStory  {
                    StoryID = 129,
                    Status = Status.Completed,
                    Tasks = new ObservableCollection<Task>{new Task { TaskID = 9 }, new Task { TaskID = 10 }}
                    },
                };
    
    
                ObservableCollection<ObservableCollection<Entity>> Document
                    = new ObservableCollection<ObservableCollection<Entity>>();
    
                Document.Add(UserStories);
                Util.SerializeObjectToXML<ObservableCollection<ObservableCollection<Entity>>>(Document, @"d:\tmp\junk.txt");
    
            }
        }
    
    }
    

    The above code produced this text file:

    <?xml version="1.0" encoding="utf-8"?>
    <ArrayOfArrayOfEntity xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <ArrayOfEntity>
        <Entity xsi:type="UserStory">
          <StoryID>127</StoryID>
          <Status>Active</Status>
          <Tasks>
            <Task>
              <TaskID>11</TaskID>
            </Task>
            <Task>
              <TaskID>12</TaskID>
            </Task>
          </Tasks>
        </Entity>
        <Entity xsi:type="UserStory">
          <StoryID>128</StoryID>
          <Status>Cancelled</Status>
          <Tasks>
            <Task>
              <TaskID>13</TaskID>
            </Task>
            <Task>
              <TaskID>14</TaskID>
            </Task>
          </Tasks>
        </Entity>
        <Entity xsi:type="UserStory">
          <StoryID>129</StoryID>
          <Status>Completed</Status>
          <Tasks>
            <Task>
              <TaskID>9</TaskID>
            </Task>
            <Task>
              <TaskID>10</TaskID>
            </Task>
          </Tasks>
        </Entity>
      </ArrayOfEntity>
    </ArrayOfArrayOfEntity>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having the problem described here: http://groups.google.com/group/microsoft.public.xml.soap/browse_thread/thread/029ee5b5d4fa2440/0895d73c5c3720a1 I am consuming a Web Service using
I am having a problem serializing via XML because 2 clases use a class
I'm having a problem writing Norwegian characters into an XML file using C#. I
I am having the problem that I cannot select a specific XML node which
I am having a problem while deserializing an XML: System.InvalidOperationException was unhandled Message=There is
I'm having problem with sending XML-data using HTTP POST to an API. If I
I've got a Flex 3 project. I'm having a problem with XML in Internet
I'm having a problem refreshing an xml file. I am bringing it in through
I am having a problem parsing xml that I receive from Web Service. The
I am having a problem trying to deserialise this XML: <?xml version=1.0 encoding=UTF-8?> <links>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.