PRB: "System.InvalidOperationException" Error While Serializing a Class Without Default Constructor (816225)
The information in this article applies to:
- Microsoft XML Classes (included with the .NET Framework 1.1)
- Microsoft XML Classes (included with the .NET Framework 1.0)
Beta Information This article discusses a Beta release of a Microsoft
product. The information in this article is provided as-is and is subject to
change without notice.
No formal product support is available from
Microsoft for this Beta product. For information about how to obtain support
for a Beta release, see the documentation that is included with the Beta
product files, or check the Web location from which you downloaded the release.
SYMPTOMSWhen you create an XmlSerializer object to serialize a class without a default constructor, you
may receive the following error message: An unhandled
exception of type 'System.InvalidOperationException' occurred in system.xml.dll
Additional information: ApplicationName.ClassName cannot be
serialized because it does not have a default public
constructor. CAUSETo be serialized by an XmlSerializer object, a class must have a default constructor. For additional
information about XML Serialization Considerations, visit the following
Microsoft Web site: WORKAROUNDTo work around this problem, add the default constructor
without parameter to the class so that the XMLSerializer can serialize the object without error. The following code
demonstrates how to add the default constructor to the class that is used in
the sample code of the "More Information" section: Visual C# .NET Code public class TestClass
{
private string UserName;
// Add the Default Constructor
public TestClass()
{
// Default Constructor without parameter
}
// Existing Constructor
public TestClass(string name)
{
UserName = name;
}
}Visual Basic .NET Code Public Class TestClass
Public UserName As String
' Add the Default Constructor
Public Sub New()
' Default Constructor without parameter
End Sub
' Existing Constructor
Public Sub New(ByVal name As String)
UserName = name
End Sub
End ClassSTATUS This
behavior is by design.REFERENCES For additional information
about XML Serialization in the .NET Framework, click the following article
number to view the article in the Microsoft Knowledge Base: 314150
INFO: Roadmap for XML
Serialization in the .NET Framework
| Modification Type: | Major | Last Reviewed: | 9/24/2003 |
|---|
| Keywords: | kbNameSpace kberrmsg kbSerial kbprb KB816225 kbAudDeveloper |
|---|
|