So I'm using XmlSerializer to serialize out a wrapper object that
contains an arbitrary number of other objects. The class definitions
listed below are made to be very generic. Some of the objects contain
other objects so when I declare my XmlSerializer I need to send along
an array of types to be included in the serialization. That works
fine until I run into 2 classes with the same name from different
asseblies. This causes an XML error stating that the similar names
are ambiguous in XML. So I went ahead and used XmlAttributeOverrides
as a parameter instead of an array of types. This causes the
ambiguous XML error to go away. Here's where the problem begins.
Before, when using an XmlSerializer with an array of types as a
parameter, it would deserialize perfectly (when there are no name
conflicts or I edit up the offending assmblies to have no name clicts
which I don't want to do). The TestObjectContainer.TestObject and
MethodToTest.Result deserialize as objects and I could tell what they
were supposed to be by looking at GetType(). However, now that I am
using XmlAttributeOverrides as a parameter, the
TestObjectContainer.TestObject and MethodToTest.Result objects
deserialize as an array of XML nodes. The rest of TestObjectContainer
and MethodToTest deserialize correctly. I call the same method to
generate the XmlAttributeOverrides object for both serializing and
deserializing. I think that there is one little step that I'm
missing, but I'm not sure what it is.
Here is the wrapper class definition:
/// <summary>
/// Contains an instance of an object to be tested. Each method
/// on the object to be tested is contained in the array of
/// MethodToTest objects.
/// </summary>
[Serializable]
public class TestObjectContainer
{
public TestObjectContainer(){}
/// <summary>
/// The object to be tested. It should be put here in the
state
/// in which it will be tested later.
/// </summary>
[XmlElement(ElementName = "TestObject")]
public Object TestObject;
/// <summary>
/// Some objects require a method to be run to set the object
up.
/// </summary>
[XmlElement(ElementName = "SetupMethod")]
public string SetupMethod;
/// <summary>
/// This is an array of MethodToTest objects. There should be
one
/// for every method that you wish to test. You can run
multiple
/// tests on the same method if you wanted to test different
/// parameters and different results.
/// </summary>
[XmlArrayItem("Method")]
public MethodToTest[] MethodsToTest;
}
Here is the class definition for the MethodToTest object:
/// <summary>
/// Contains the necessary info to test a given method including
/// the method name, the method parameters and the result object.
/// </summary>
public class MethodToTest
{
public MethodToTest(){}
/// <summary>
/// Name of the method to call.
/// </summary>
[XmlElement(ElementName = "MethodName")]
public string MethodName;
/// <summary>
/// The parameters to be sent to the method. Can be null.
/// </summary>
[XmlArrayItem("Parameter")]
public Object[] Parameters;
/// <summary>
/// The expected result object.
/// </summary>
[XmlElement(ElementName = "Result")]
public Object Result;
}
Here is the method I'm using to create the XmlAttributeOverrides
object:
/// <summary>
/// Determines all the types to be included.
/// </summary>
/// <returns>Return an array of types included in the
object.</returns>
private XmlAttributeOverrides GetOverridesToBeSerialized()
{
// Let's find the assembly used by ExternalOrder.
System.Reflection.Assembly bizAssembly =
typeof(ExternalOrder).Assembly;
// Get all the types in the assembly.
System.Type[] bizTypes = bizAssembly.GetTypes();
ArrayList objectTypes = new ArrayList();
XmlAttributeOverrides attrOverrides = new XmlAttributeOverrides();
foreach (System.Type type in bizTypes)
{
// Skip interfaces.
if (!type.IsInterface)
{
objectTypes.Add(type);
}
}
/* These classes aren't in the same assembly used
above,
but are necessary. Some of the class names
contained in
these classes conflict with the class names in the
above
assembly. */
objectTypes.Add(typeof(Address));
objectTypes.Add(typeof(MsgOrder));
foreach (System.Type type in objectTypes)
{
XmlElementAttribute attr = new XmlElementAttribute(type.Name,
type);
// Create the XmlAttributes class.
XmlAttributes attrs = new XmlAttributes();
attrs.XmlElements.Add(attr);
attrOverrides.Add(type, type.Name, attrs);
}
return attrOverrides;
}