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



The information in this article applies to:

  • Microsoft Visual C# .NET (2003)
  • Microsoft Visual C# .NET (2002)
  • Microsoft .NET Framework 1.1
  • Microsoft .NET Framework 1.0
  • Microsoft Visual C# 2005, Express Edition

This article was previously published under Q330592
This article refers to the following Microsoft .NET Framework Class Library namespace:
System.Xml.Serialization

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.

CAUSE

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

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() {}
}

STATUS

This behavior is by design.

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.

REFERENCES

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

Modification Type:MajorLast Reviewed:1/17/2006
Keywords:kberrmsg kbprb KB330592 kbAudDeveloper kbAudITPRO