PRB: You Receive a System.InvalidOperationException Error Message While You Serialize an Object That Has Two Arrays That Both Have Unqualified Items as Public Members (816224)



The information in this article applies to:

  • Microsoft .NET Framework 1.1
  • Microsoft .NET Framework 1.0
  • Microsoft Visual C# .NET (2003)
  • Microsoft Visual C# .NET (2002)

CAUSE

By using ElementName, you can make the name of the generated XML element different from the member's identifier. When you set a single ElementName to more than one class member (that is, multiple class members have the same ElementName), the generated XML document uses XML namespaces to distinguish between identically named members. When the namespace is not present, you receive the error message that is mentioned in the "Symptoms" section.

WORKAROUND

To work around this problem, add the namespace in the XmlArrayItemAttribute declaration. Because the namespace is specified, you have to set the Form property to Qualified or None as shown in the following code.

Note For the sample that is described in the "More Information" section of this article, replace the ClassToBeSerialized class with the following code:

Visual C# .NET

public class ClassToBeSerialized 
{
   [XmlArrayAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
   [XmlArrayItemAttribute("item", Form=System.Xml.Schema.XmlSchemaForm.Qualified, Namespace="http://kbarticles.com/", IsNullable=false)]
   public FirstClass[] firstArrayObj = new FirstClass[2] {new FirstClass(), new FirstClass()};

   [XmlArrayAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified )]
   [XmlArrayItemAttribute("item", Form=System.Xml.Schema.XmlSchemaForm.Qualified, Namespace="http://kbarticles.com/", IsNullable=false)]
   public SecondClass[] secondArrayObj = new SecondClass[2] {new SecondClass(), new SecondClass()};
}

Visual Basic .NET

Public Class ClassToBeSerialized
   <XmlArrayAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified), _
   XmlArrayItemAttribute("item", Form:=System.Xml.Schema.XmlSchemaForm.Qualified, Namespace:="http://kbarticles.com/", IsNullable:=False)> _
   Public firstArrayObj As FirstClass() = New FirstClass() {New FirstClass(), New FirstClass()}

   <XmlArrayAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified), _
   XmlArrayItemAttribute("item", Form:=System.Xml.Schema.XmlSchemaForm.Qualified, Namespace:="http://kbarticles.com/", IsNullable:=False)> _
   Public secondArrayObj As SecondClass() = New SecondClass() {New SecondClass(), New SecondClass()}
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. Select either Visual C# .NET or Visual Basic .NET under Project Types. Select Console Application under Templates.
  4. Name the project MyConsoleApplication, and then click OK.
  5. Replace the existing code with the following code (either Visual C# .NET or Visual Basic .NET as appropriate):

    Visual C# .NET Code
    using System.Diagnostics;
    using System.Xml.Serialization;
    using System;
    using System.ComponentModel;
    
    public class MainClass 
    {
       static void Main() 
       {
          ClassToBeSerialized myTestObj = new ClassToBeSerialized();
          XmlSerializer mySerializer = new XmlSerializer(typeof(ClassToBeSerialized));
          mySerializer.Serialize(Console.Out, myTestObj);
          Console.Read();
       }
    }
    
    [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://kbarticles.com/")]
    public class FirstClass 
    {
       public string strMem;
       public int intMem;
    }
    
    [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://kbarticles.com/")]
    public class SecondClass 
    {
       public string strMem;
       public int intMem;
    }
    
    // The object of this class is used for serialization.
    public class ClassToBeSerialized 
    {
       [XmlArrayAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
       [XmlArrayItemAttribute("item", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=false)]
       public FirstClass[] firstArrayObj = new FirstClass[2] {new FirstClass(), new FirstClass()};
    
       [XmlArrayAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified )]
       [XmlArrayItemAttribute("item", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=false)]
       public SecondClass[] secondArrayObj = new SecondClass[2] {new SecondClass(), new SecondClass()};
    }
    
    Visual Basic .NET Code
    Imports System.Diagnostics
    Imports System.Xml.Serialization
    Imports System
    Imports System.ComponentModel
    
    ' The object of this class is used for serialization.
    Public Class ClassToBeSerialized
       <XmlArrayAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified), _
       XmlArrayItemAttribute("item", Form:=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable:=False)> _
       Public firstArrayObj As FirstClass() = New FirstClass() {New FirstClass(), New FirstClass()}
    
       <XmlArrayAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified), _
       XmlArrayItemAttribute("item", Form:=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable:=False)> _
       Public secondArrayObj As SecondClass() = New SecondClass() {New SecondClass(), New SecondClass()}
    
    End Class
    
    <System.Xml.Serialization.XmlTypeAttribute(Namespace:="http://kbarticles.com/")> _
    Public Class FirstClass
       Public strMem As String
       Public intMem As Integer
    End Class
    
    <System.Xml.Serialization.XmlTypeAttribute(Namespace:="http://kbarticles.com/")> _
    Public Class SecondClass
       Public strMem As String
       Public intMem As Integer
    End Class
    
    Module MyTestModule
    
       Sub Main()
          Dim myTestObj As New ClassToBeSerialized()
          Dim mySerializer As New XmlSerializer(GetType(ClassToBeSerialized))
          mySerializer.Serialize(Console.Out, myTestObj) ' Serializing the object
          Console.ReadLine()
       End Sub
    
    End Module
    
  6. In Visual Studio .NET Solution Explorer, right-click MyConsoleApplication, and then click Add Reference.
  7. On the .NET tab, click System.Web.Services.dll, click Select, and then click OK.
  8. On the Debug menu, click Start.

    You receive the exception that is described in the "Symptoms" section.
  9. Replace the code for the ClassToBeSerialized class with the code that is described in the "Workaround" section, and then restart the application.

REFERENCES

For additional information about XML serialization, click the following article numbers to view the articles in the Microsoft Knowledge Base:

323503 WebCast: XML Serialization and Sample Code

314150 INFO: Roadmap for XML Serialization in the .NET Framework


Modification Type:MinorLast Reviewed:9/5/2003
Keywords:kberrmsg kbXML kbSerial kbprb KB816224 kbAudDeveloper