Microsoft KB Archive/330592

= Error message when you serialize a class by using the XMLSerializer class: &quot;System.InvalidOperationException&quot; =

Article ID: 330592

Article Last Modified on 12/11/2006

-

APPLIES TO


 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition
 * 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 ' '.

where  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
 Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005 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.  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(&quot;Hello World!&quot;);

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

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

}   }

} Note In Visual Studio 2005, replace the existing code in the Program.cs file.  Compile, and then run the code.</li></ol>

You receive the error message that is mentioned in the &quot;Symptoms&quot; section of this article.

<div class="references_section">