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. CAUSEThe 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.REFERENCES For more information about serializing, visit the following
Microsoft Developer Network (MSDN) Web sites:
Modification Type: | Major | Last Reviewed: | 1/17/2006 |
---|
Keywords: | kberrmsg kbprb KB330592 kbAudDeveloper kbAudITPRO |
---|
|