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

HI,

I have a class that looks like this:

[Serializable]

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'."
Source="System.Xml"
StackTrace:
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()



Answer this question

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

  • Jim Cruis

    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



  • Andy Bounsall

    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.

  • SFrench

    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).

  • emmarussell

    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...

     

    [Serializable]

    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]

    }


  • hartinary

    What does the innerException say (if more than 1 the innermost)

    Generally that exception includes an inner exception that explains the problem more explicitly


  • sahan

    I stand corrected. I guess its understandable why dictionaries would be difficult to support in XML.

  • zippy1981

    Quick sanity check, is the NewsItem Serializable



  • luker

    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!

    Thanks


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