// Is there any way to override the XML Serialization of the following
SimpleClass
// to turn off the DefaultValue of a boolean? Sure, I can override
the DefaultValue from
// true to false, but that is not what I want/need. I really need to
just override the fact
// that the developer ever supplied a DefaultValue. Setting an
override DefaultValue of null
// or "new object()" errors. Any help would be appreciated.
using System;
using System.Collections;
using System.ComponentModel;
using System.IO;
using System.Xml.Serialization;
namespace XMLStub
{
/// <summary>A simple class to be XML Serialized.</summary>
public class SimpleClass
{
[DefaultValue(true)]
public bool TheBool = true;
[DefaultValue("InitialValue")]
public string TheString = "InitialValue";
public SimpleClass(bool NewBool, string NewString)
{
TheBool = NewBool;
TheString = NewString;
}
public SimpleClass()
{
}
}
/// <summary>A simple class that holds a collection of
SimpleClass.</summary>
public class SimpleCollection : CollectionBase
{
public SimpleClass this[int Index]
{
get { return((SimpleClass)List[Index]); }
}
public void Add(SimpleClass NewSimpleClass)
{
List.Add(NewSimpleClass);
}
}
class gMain
{
[STAThread]
static void Main(string[] args)
{
//SimpleClass InstanceToSerialize = new SimpleClass();
SimpleCollection InstanceToSerialize = new SimpleCollection();
InstanceToSerialize.Add(new SimpleClass(true, "I am true"));
InstanceToSerialize.Add(new SimpleClass(false, "I am false"));
InstanceToSerialize.Add(new SimpleClass(true, "true #2"));
InstanceToSerialize.Add(new SimpleClass(false, "false #2"));
// Serialize with the pre configured default values.
XmlSerializer Serializer = new
XmlSerializer(InstanceToSerialize.GetType());
StringWriter XMLStringWriter = new StringWriter();
Serializer.Serialize(XMLStringWriter, InstanceToSerialize);
Console.WriteLine("\nWITHOUT Overrides: \n" +
XMLStringWriter.ToString());
// Attempt to serialize as if there were no default values set.
XmlAttributeOverrides ExplicitOverrides = new
XmlAttributeOverrides();
// Setup the override default value for TheString.
XmlAttributes TheStringAttributes = new XmlAttributes();
TheStringAttributes.XmlDefaultValue = "SOME TEXT THE STRING WILLL
NEVER CONTAIN";
ExplicitOverrides.Add(typeof(SimpleClass), "TheString",
TheStringAttributes);
// Setup the override default value for TheBool.
XmlAttributes TheBoolAttributes = new XmlAttributes();
TheBoolAttributes.XmlDefaultValue = false; // Works, but
only for a single instance, fails for an array or collection.
// TheBoolAttributes.XmlDefaultValue = null; // Throws a
file not found exception
// TheBoolAttributes.XmlDefaultValue = new object(); // Throws an
InvalidOperationException: The default value type, System.Object, is
unsupported.
// TheBoolAttributes.XmlDefaultValue = ""; // Throws a
file not found exception
// TheBoolAttributes.XmlDefaultValue = 6; // Throws a
file not found exception
ExplicitOverrides.Add(typeof(SimpleClass), "TheBool",
TheBoolAttributes);
XmlSerializer SerializerOverrides = new
XmlSerializer(InstanceToSerialize.GetType(), ExplicitOverrides);
XMLStringWriter = new StringWriter();
SerializerOverrides.Serialize(XMLStringWriter,
InstanceToSerialize);
Console.WriteLine("\nWITH Overrides: \n" +
XMLStringWriter.ToString());
}
}
}
// Sample Output:
//WITHOUT Overrides:
//<?xml version="1.0" encoding="utf-16"?>
//<ArrayOfSimpleClass xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
//<SimpleClass>
//<TheString>I am true</TheString>
//</SimpleClass>
//<SimpleClass>
//<TheBool>false</TheBool>
//<TheString>I am false</TheString>
//</SimpleClass>
//<SimpleClass>
//<TheString>true #2</TheString>
//</SimpleClass>
//<SimpleClass>
//<TheBool>false</TheBool>
//<TheString>false #2</TheString>
//</SimpleClass>
//</ArrayOfSimpleClass>
//
//WITH Overrides:
//<?xml version="1.0" encoding="utf-16"?>
//<ArrayOfSimpleClass xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
//<SimpleClass>
//<TheBool>true</TheBool>
//<TheString>I am true</TheString>
//</SimpleClass>
//<SimpleClass>
//<TheString>I am false</TheString>
//</SimpleClass>
//<SimpleClass>
//<TheBool>true</TheBool>
//<TheString>true #2</TheString>
//</SimpleClass>
//<SimpleClass>
//<TheString>false #2</TheString>
//</SimpleClass>
//</ArrayOfSimpleClass>