Article ID: 330592 - View products that this article applies to.
This article was previously published under Q330592
This article refers to the following Microsoft .NET Framework Class Library namespace:
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:
where class indicates the class that the XmlSerializer class tried to serialize.
An unhandled exception of type 'System.InvalidOperationException' occurred in system.xml.dll Additional information: There was an error reflecting 'class'.
The System.InvalidOperationException exception is raised because the default constructor is not defined for the class that the XmlSerializer tried to serialize.
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):
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
For more information about serializing, visit the following Microsoft Developer Network (MSDN) Web sites:
Article ID: 330592 - Last Review: December 11, 2006 - Revision: 3.5