Microsoft KB Archive/816224

= 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 =

Article ID: 816224

Article Last Modified on 9/5/2003

-

APPLIES TO


 * Microsoft .NET Framework 1.1
 * Microsoft .NET Framework 1.0
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition

-



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 &quot;Symptoms&quot; 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 &quot;More Information&quot; 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(&quot;item&quot;, Form=System.Xml.Schema.XmlSchemaForm.Qualified, Namespace=&quot;http://kbarticles.com/&quot;, IsNullable=false)] public FirstClass[] firstArrayObj = new FirstClass[2] {new FirstClass, new FirstClass};

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

Visual Basic .NET
Public Class ClassToBeSerialized  _ Public firstArrayObj As FirstClass = New FirstClass {New FirstClass, New FirstClass}

 _ Public secondArrayObj As SecondClass = New SecondClass {New SecondClass, New SecondClass} 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. Select either Visual C# .NET or Visual Basic .NET under Project Types. Select Console Application under Templates. Name the project MyConsoleApplication, and then click OK.  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=&quot;http://kbarticles.com/&quot;)] public class FirstClass {  public string strMem; public int intMem; }

[System.Xml.Serialization.XmlTypeAttribute(Namespace=&quot;http://kbarticles.com/&quot;)] 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(&quot;item&quot;, 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(&quot;item&quot;, 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(&quot;item&quot;, 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(&quot;item&quot;, 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:=&quot;http://kbarticles.com/&quot;)> _ Public Class FirstClass Public strMem As String Public intMem As Integer End Class

<System.Xml.Serialization.XmlTypeAttribute(Namespace:=&quot;http://kbarticles.com/&quot;)> _ 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 </li> In Visual Studio .NET Solution Explorer, right-click MyConsoleApplication, and then click Add Reference.</li> On the .NET tab, click System.Web.Services.dll, click Select, and then click OK.</li> On the Debug menu, click Start.

You receive the exception that is described in the &quot;Symptoms&quot; section.</li> Replace the code for the ClassToBeSerialized class with the code that is described in the &quot;Workaround&quot; section, and then restart the application.</li></ol>

<div class="references_section">