Serialize Strong Typed Collection. "There was an error reflecting type.."


  • HI,

    I have a class that looks like this:


    public class NewsItems : System.Collections.Generic.List<NewsItem>



    Any it works just fine for 99% of my needs... Here is the 1% problem, when I want to write out the contents of my collection to XML, I get the following error (pasted below) anyone know how to remedy this?

    System.InvalidOperationException was unhandled
      Message="There was an error reflecting type 'NewsItems'."
           at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, Boolean repeats, Boolean openModel)
           at System.Xml.Serialization.XmlReflectionImporter.ImportElement(TypeModel model, XmlRootAttribute root, String defaultNamespace)
           at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(Type type, XmlRootAttribute root, String defaultNamespace)
           at System.Xml.Serialization.XmlSerializer..ctor(Type type, String defaultNamespace)
           at System.Xml.Serialization.XmlSerializer..ctor(Type type)
           at Driver.Form1.button1_Click(Object sender, EventArgs e) in E:\eZamiRSS\Driver\Form1.cs:line 34
           at System.Windows.Forms.Control.OnClick(EventArgs e)
           at System.Windows.Forms.Button.OnClick(EventArgs e)
           at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
           at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
           at System.Windows.Forms.Control.WndProc(Message& m)
           at System.Windows.Forms.ButtonBase.WndProc(Message& m)
           at System.Windows.Forms.Button.WndProc(Message& m)
           at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
           at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
           at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
           at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
           at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
           at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
           at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
           at System.Windows.Forms.Application.Run(Form mainForm)
           at Driver.Program.Main() in E:\eZamiRSS\Driver\Program.cs:line 17
           at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
           at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
           at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           at System.Threading.ThreadHelper.ThreadStart()

    Friday, February 03, 2006 2:37 AM

All replies

  • Quick sanity check, is the NewsItem Serializable?


    Friday, February 03, 2006 2:56 AM
  • Yes it is, (see below) unless there is another attrubute I need to add to the methods/fields/properties?

    One thing I can say is when passed through web service, the newsitems is serialized just fine...



    public class NewsItem


    String publishDate;

    String originURL;

    String link;

    String comments;

    String title;

    String description;

    String feedID;

    String author;

    String rawXML;

    Dictionary<String, String> otherValues;

    [public get/set properties for each field is only other thing in class]


    Friday, February 03, 2006 3:23 AM
  • What does the innerException say? (if more than 1 the innermost)

    Generally that exception includes an inner exception that explains the problem more explicitly
    Saturday, February 04, 2006 12:58 PM
  • Yes, I only dug down 1 level, and the inner had a inner, which said that the property I have of type Dictionary<String,String> was unserializable!



    Tuesday, February 07, 2006 1:46 AM
  • Aha...I knew I had seen this somewhere else before. The SOAP/XML Serializer doesn't support Generics, the team will be implementing all of that jazz in WCF. See this thread for more info.
    Thursday, February 09, 2006 8:55 AM
  • Mike - The Xml Serializer and SOAP Serializer are not the same thing. The SOAP serailizer is really a formatter used for "deep" serialization, and can be used in remoting along with the binary formatter. The Xml Serializer is used to serialize/deserialize parameters passed to web methods. The Xml Serializer can handle generic collections in Framework 2.0, but it is not capable of serializing dictionaries (including generic ones).
    Wednesday, May 03, 2006 2:49 AM
  • I stand corrected. I guess its understandable why dictionaries would be difficult to support in XML.
    Wednesday, May 03, 2006 4:02 AM
  • Starting in .NET 3.0 you can use the DataContractSerializer instead.  The DataContractSerializer supports generics and dictionaries.  You can find a list of the types supports here: http://msdn2.microsoft.com/en-us/library/ms731923.aspx.


    Daniel Roth

    Monday, December 31, 2007 5:39 PM
  • HI All,

    I had similar situation when sending the object to MSMQ, but setting up queue formatter as

    Queue.Formatter =


    new BinaryMessageFormatter(); resolved above issue...

    hope it helps,


    Thursday, October 28, 2010 6:21 AM
  • The problem is with one of the items you added to the dictionary - it is not serializable.

    When the dictionary is empty - does serialization work?

    WCF Security, Interoperability And Performance Blog
    Thursday, October 28, 2010 9:51 PM