Microsoft KB Archive/816225

= PRB: &quot;System.InvalidOperationException&quot; Error While Serializing a Class Without Default Constructor =

Article ID: 816225

Article Last Modified on 5/12/2007

-

APPLIES TO


 * Microsoft .NET Framework 1.1
 * Microsoft .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.



SYMPTOMS
When 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.



CAUSE
To 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:

http://msdn2.microsoft.com/en-us/library/182eeyhh(vs.71).aspx



WORKAROUND
To 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 &quot;More Information&quot; 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 Class



STATUS
This behavior is by design.



Steps to Reproduce the Behavior
 Start Microsoft Visual Studio .NET. On the File menu, point to New, and then click Project. Under Project Types, click Visual C# .NET or Visual Basic .NET, and then click Console Application under Templates. Name the project ConstructorTestingApplication, and then click OK.  Replace the existing code with the following code:

Visual C# .NET Code
using System; using System.Xml; using System.Xml.Schema; using System.Xml.Serialization;

namespace ConstructorTestingApplication {   public class TestClass {       public string UserName;

// Constructor, No default constructor public TestClass(string name) {           UserName = name; }   }    // Class having Main Method public class MainClass {       public static void Main {           // initializing XmlSerializer object with typeof TestClass XmlSerializer ser = new XmlSerializer(typeof(TestClass)); }   } }

Visual Basic .NET Code
Imports System Imports System.Xml Imports System.Xml.Schema Imports System.Xml.Serialization

Public Class TestClass Public UserName As String

' Constructor, No default constructor Public Sub New(ByVal name As String) UserName = name End Sub End Class Module Module1

Sub Main ' initializing XmlSerializer object with typeof TestClass Dim ser As New XmlSerializer(GetType(TestClass)) End Sub

End Module </li> On the Debug menu, click Start. You receive the error message that is described in the &quot;Symptoms&quot; section.</li></ol>

<div class="references_section">