Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

Error message when you serialize a class by using the XMLSerializer class: "System.InvalidOperationException"


This article refers to the following Microsoft .NET Framework Class Library namespace:
System.Xml.Serialization

↑ Back to the top


Symptoms

When you try to use the XmlSerializer class to serialize a class that does not have a public default constructor, you may receive the following System.InvalidOperationException exception error message:
An unhandled exception of type 'System.InvalidOperationException' occurred in system.xml.dll Additional information: There was an error reflecting 'class'.
where
class indicates the class that the XmlSerializer class tried to serialize.

↑ Back to the top


Cause

The System.InvalidOperationException exception is raised because the default constructor is not defined for the class that the XmlSerializer tried to serialize.

↑ Back to the top


Resolution

To resolve this problem, define a parameterless constructor.

Add a public default constructor to the class that you want to serialize. The following code demonstates a parameterless constructor for a class (in this samplem, the class is named SerializerTest):
[Serializable]
public class SerializerTest
{
public SerializerTest(string data)
{
this.data = data;
}

public string data;

//Add a parameterless constructor.
public SerializerTest() {}
}

↑ Back to the top


Status

This behavior is by design.

↑ Back to the top


More Information

The System.InvalidOperationException error message is used to indicate that a method was not invoked for reasons other than invalid arguments. The System.InvalidOperationException error for the XmlSerializer class Serialize method is caused when you try to serialize a class that does not have a default constructor or a public modifier.

An instance of the class is created when the class is deserialized with the XmlSerializer.Deserialize method. The default constructor that is provided by the class is invoked by the Deserialize method to create the instance. Even if additional constructors are defined for the class, only the default constructor is invoked.

Steps to Reproduce the Behavior

  1. Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005
  2. Click Visual C# Projects under
    Project Types, and then click Console Application under Templates.

    Note In Visual Studio 2005, click Visual C# under Project Types.
  3. In the generated default Class1.cs, replace the existing code with the following code:
    using System;
    using System.Xml;
    using System.Xml.Serialization;
    using System.Runtime.Serialization;
    using System.IO;


    namespace ConsoleTester
    {
    [Serializable]
    public class SerializerTest
    {
    public SerializerTest(string data)
    {
    this.data = data;
    }

    public string data;

    }

    class ConsoleMain
    {
    [STAThread]
    static void Main(string[] args)
    {
    SerializerTest testInstance = new SerializerTest("Hello World!");

    XmlSerializer ser = new XmlSerializer(typeof(SerializerTest));

    TextWriter writer = new StreamWriter("C:\\test.xml");
    ser.Serialize(writer, testInstance);
    writer.Close();

    }
    }

    }
    Note In Visual Studio 2005, replace the existing code in the Program.cs file.
  4. Compile, and then run the code.
You receive the error message that is mentioned in the "Symptoms" section of this article.

↑ Back to the top


References

For more information about serializing, visit the following Microsoft Developer Network (MSDN) Web sites:

↑ Back to the top


Keywords: kbvcs2005rtmapplies, kbvcs2005rtmsweep, kberrmsg, kbprb, kb

↑ Back to the top

Article Info
Article ID : 330592
Revision : 6
Created on : 6/10/2019
Published on : 6/10/2019
Exists online : False
Views : 111