Microsoft KB Archive/815105

= A null ArrayList member is initialized to a zero length ArrayList class after it is deserialized =

Article ID: 815105

Article Last Modified on 2/5/2004

-

APPLIES TO


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

-





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 where you downloaded the release.



SYMPTOMS
When you serialize an object that contains a null ArrayList class object as a public member, as expected the tags for the ArrayList class member is not present in the serialized XML file. However, after deserialization, you may notice that the ArrayList class instance is not null. Instead, the ArrayList class member is initialized to a zero length ArrayList class.



CAUSE
The XML Serializer initializes all the public field members of the serialized class during deserialization. Therefore, the ArrayList class member is initialized to a zero length ArrayList.



WORKAROUND
To work around this problem, modify the ArrayList class instance as null after deserialization. To do this, add the following line of code after deserialization:

Microsoft Visual C# .NET code: // Make an ArrayList instance as null after deserialization. obj.MyArrayList = null; Microsoft Visual Basic .NET code:

' Make an ArrayList instance as null after deserialization. obj.MyArrayList = Nothing



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. Under Templates, click Console Application. Name the project as MyConsoleApplication, and then click OK.  Replace the existing code with the following code:

Visual C# .NET code using System; using System.IO; using System.Collections; using System.Xml; using System.Xml.Serialization;

namespace SerializationTestingApplication {  //    The object of this class is used for serialization public class ClsTest {     public String Name; public ArrayList List; }

class Tests {     // The Main function and the testing is done here. static void Main(string[] args) {        ClsTest obj = new ClsTest; XmlSerializer mySerializer = new XmlSerializer(obj.GetType); StringWriter strWriter = new StringWriter; XmlTextWriter writer = new XmlTextWriter(strWriter);

writer.Formatting = Formatting.Indented; writer.Indentation = 2; obj.Name = &quot;John&quot;;

// Serialization mySerializer.Serialize(writer, obj); Console.WriteLine(strWriter.ToString); Console.WriteLine(&quot;\nRead back&quot;);

// Deserialization obj = (ClsTest) mySerializer.Deserialize(new StringReader(strWriter.ToString));

// Check the deserialized object by serializing. mySerializer.Serialize(Console.Out, obj); Console.WriteLine; Console.Read; }  } }  Visual Basic .NET code Imports System Imports System.IO Imports System.Collections Imports System.Xml Imports System.Xml.Serialization

' The object of this class is used for serialization. Public Class ClsTest Public Name As String Public List As ArrayList End Class

Module Module1

' The Main function and the testing is done here. Sub Main Dim obj As New ClsTest Dim mySerializer As New XmlSerializer(GetType(ClsTest)) Dim strWriter As New StringWriter Dim writer As New XmlTextWriter(strWriter)

writer.Formatting = Formatting.Indented writer.Indentation = 2

obj.Name = &quot;John&quot;

' Serialization mySerializer.Serialize(writer, obj) Console.WriteLine(strWriter.ToString) Console.WriteLine(vbCrLf & &quot;Read back&quot;)

' Deserialization obj = mySerializer.Deserialize(New StringReader(strWriter.ToString))

' Check the deserialized object by serializing. mySerializer.Serialize(Console.Out, obj) Console.WriteLine Console.Read End Sub

End Module </li> On the Debug menu, click Start. You notice the behavior that is described in the &quot;Symptoms&quot; section of this article. In the console output for Read Back, you find an empty tag for  .</li></ol>

<div class="references_section">