Fixed it by using an XmlIgnore on the public List<Server> and instead serializing "public Server[] ServerArray" which just converts to/from the list object. It looks like there is a bug in the List<> implementation. I would be curious to try other versions of the .Net 2.0 framework to see when it changed (SP1/SP2), but not enought to mess with my system right now.
Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using System.Xml;
using System.Xml.Schema;
namespace XmlTests
{
[System.Xml.Serialization.XmlRoot("settings", Namespace = "", IsNullable = true)]
public class Settings
{
private List<Server> servers = new List<Server>();
public Settings()
{
// If you didn't initialize the private variable "servers" in the declaration, you must do it here.
}
[COLOR="#0000FF"][System.Xml.Serialization.XmlArray("servers")]
[System.Xml.Serialization.XmlArrayItem("server", typeof(Server), IsNullable = true)][/COLOR]
[COLOR="#008000"]// Comment out the above two lines and uncomment the next line if you don't want all the <server> elements grouped within a <servers> element
//[System.Xml.Serialization.XmlElementAttribute("server", typeof(Server), Form = System.Xml.Schema.XmlSchemaForm.Unqualified)][/COLOR]
[COLOR="#0000FF"]public Server[] ServersArray
{
get { return this.servers.ToArray(); }
set { if (value != null) this.servers = new List<Server>(value); }
}[/COLOR]
[COLOR="#B22222"][XmlIgnore]
public List<Server> Servers
{
get { return this.servers; }
set { this.servers = value; }
}[/COLOR]
public void ReadFrom(string FileName)
{
FileInfo fi = new FileInfo(FileName);
if (fi.Exists)
{
using (System.IO.FileStream fs = fi.OpenRead())
{
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(this.GetType());
System.Xml.XmlReaderSettings rs = new System.Xml.XmlReaderSettings();
rs.ProhibitDtd = false;
rs.XmlResolver = null;
System.Xml.XmlReader reader = System.Xml.XmlReader.Create(fs, rs);
Settings tmp = (Settings)serializer.Deserialize(reader);
this.Servers = tmp.Servers;
}
}
}
public void WriteTo(string FileName)
{
FileInfo fi = new FileInfo(FileName);
using (System.IO.FileStream fs = fi.Create())
{
using (System.Xml.XmlTextWriter txw = new System.Xml.XmlTextWriter(fs, Encoding.GetEncoding("ISO-8859-1")))
{
System.Xml.Serialization.XmlSerializerNamespaces customNamespace = new System.Xml.Serialization.XmlSerializerNamespaces();
customNamespace.Add("", "");
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(this.GetType());
txw.Formatting = System.Xml.Formatting.Indented;
txw.Indentation = 2;
txw.WriteStartDocument();
serializer.Serialize(txw, this, customNamespace);
}
}
}
// If you don't want to use "using" statements.
public void WriteTo2(string FileName)
{
System.Xml.Serialization.XmlSerializerNamespaces namespaces = new System.Xml.Serialization.XmlSerializerNamespaces();
namespaces.Add("", "");
System.Xml.XmlTextWriter xmlTextWriter = new System.Xml.XmlTextWriter(FileName, Encoding.GetEncoding("ISO-8859-1"));
System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(this.GetType());
xmlTextWriter.Formatting = System.Xml.Formatting.Indented;
xmlTextWriter.Indentation = 2;
xmlTextWriter.WriteStartDocument();
xmlSerializer.Serialize((System.Xml.XmlWriter)xmlTextWriter, this, namespaces);
xmlTextWriter.Close();
}
// Static method example
public static Settings ReadFromStatic(string FileName)
{
try
{
FileInfo fi = new FileInfo(FileName);
if (fi.Exists)
{
using (System.IO.FileStream fs = fi.OpenRead())
{
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(Settings));
System.Xml.XmlReaderSettings rs = new System.Xml.XmlReaderSettings();
rs.ProhibitDtd = false;
rs.XmlResolver = null;
System.Xml.XmlReader reader = System.Xml.XmlReader.Create(fs, rs);
Settings tmp = (Settings)serializer.Deserialize(reader);
return tmp;
}
}
}
catch { }
return null;
}
public static void WriteTo(Settings settings, string FileName)
{
FileInfo fi = new FileInfo(FileName);
using (System.IO.FileStream fs = fi.Create())
{
using (System.Xml.XmlTextWriter txw = new System.Xml.XmlTextWriter(fs, Encoding.GetEncoding("ISO-8859-1")))
{
System.Xml.Serialization.XmlSerializerNamespaces customNamespace = new System.Xml.Serialization.XmlSerializerNamespaces(); customNamespace.Add("", "");
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(settings.GetType());
txw.Formatting = System.Xml.Formatting.Indented;
txw.Indentation = 2;
txw.WriteStartDocument();
serializer.Serialize(txw, settings, customNamespace);
}
}
}
}
[XmlType]
public class Server
{
private string name = string.Empty;
public Server() { }
public Server(string Name) { this.name = Name; }
[System.Xml.Serialization.XmlElement("name", IsNullable = false)]
public string Name
{
get { return this.name; }
set { this.name = value; }
}
}
}