A null ArrayList member is initialized to a zero length ArrayList class after it is deserialized (815105)



The information in this article applies to:

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

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.

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. Under Templates, click Console Application.
  4. Name the project as MyConsoleApplication, and then click OK.
  5. 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 = "John";
    
             // Serialization 
             mySerializer.Serialize(writer, obj);
             Console.WriteLine(strWriter.ToString());
             Console.WriteLine("\nRead back");
    
             // 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 = "John"
    
          ' Serialization 
          mySerializer.Serialize(writer, obj)
          Console.WriteLine(strWriter.ToString())
          Console.WriteLine(vbCrLf & "Read back")
    
          ' 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
  6. On the Debug menu, click Start. You notice the behavior that is described in the "Symptoms" section of this article. In the console output for Read Back, you find an empty tag for <list />.

REFERENCES

For additional information, 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:2/5/2004
Keywords:kbNameSpace kbCollections kbXML kbprb KB815105 kbAudDeveloper