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.

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:

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 "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 Class

STATUS

This behavior is by design.

MORE INFORMATION

Steps to Reproduce the Behavior

  1. Start Microsoft Visual Studio .NET.
  2. On the File menu, point to New, and then click Project.
  3. Under Project Types, click Visual C# .NET or Visual Basic .NET, and then click Console Application under Templates.
  4. Name the project ConstructorTestingApplication, and then click OK.
  5. 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
  6. On the Debug menu, click Start. You receive the error message that is described in the "Symptoms" section.

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:MajorLast Reviewed:9/24/2003
Keywords:kbNameSpace kberrmsg kbSerial kbprb KB816225 kbAudDeveloper