// Can anyone explain the bizarre XML Serialization behavior seen in
the following code.
// It seems that a collection can not be XML Serialized if it contains
an Add() method
// with a single parameter where that parameter is of a type in the
System.Data namespace
// (or a class derived from something in the System.Data namespace).
// Is there any way to prevent this from blowing up?
using System;
using System.Data;
using System.Collections;
using System.IO;
using System.Xml.Serialization;
namespace XMLStub
{
/// <summary>A simple class that holds a collection of
ints.</summary>
public class IntCollection : CollectionBase
{
public int this[int Index]
{
get { return((int)List[Index]); }
}
public void Add(int NewInt)
{
List.Add(NewInt);
}
public void Add(string XXX) { } // Can be
serialized
public void Add(Type XXX) { } // Can be
serialized
public void Add(IntCollection XXX) { } // Can be
serialized
public void Add(DataColumn XXX, bool XXXBool) { } // Can be
serialized
// Simply uncommenting any of these methods causes the
XMLSerialization to fail
// public void Add(DataColumn XXX) { } // Throws when
serializing
// public void Add(DataTable XXX) { } // Throws when
serializing
// public void Add(DataSet XXX) { } // Throws when
serializing
// public void Add(Constraint XXX) { } // Throws when
serializing
// public void Add(VersionNotFoundException XXX) { } // Throws when
serializing
}
class gMain
{
[STAThread]
static void Main(string[] args)
{
XmlSerializer Serializer = new
XmlSerializer(typeof(IntCollection));
StringWriter XMLStringWriter = new StringWriter();
IntCollection TestCollection = new IntCollection();
Serializer.Serialize(XMLStringWriter, TestCollection);
Console.WriteLine("Serialized TestCollection:");
Console.WriteLine(XMLStringWriter.ToString());
}
}
}