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 XmlAttributeOve rrides
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 TestObjectConta iner.TestObject and
MethodToTest.Re sult deserialize as objects and I could tell what they
were supposed to be by looking at GetType(). However, now that I am
using XmlAttributeOve rrides as a parameter, the
TestObjectConta iner.TestObject and MethodToTest.Re sult objects
deserialize as an array of XML nodes. The rest of TestObjectConta iner
and MethodToTest deserialize correctly. I call the same method to
generate the XmlAttributeOve rrides 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 TestObjectConta iner
{
public TestObjectConta iner(){}
/// <summary>
/// The object to be tested. It should be put here in the
state
/// in which it will be tested later.
/// </summary>
[XmlElement(Elem entName = "TestObject ")]
public Object TestObject;
/// <summary>
/// Some objects require a method to be run to set the object
up.
/// </summary>
[XmlElement(Elem entName = "SetupMetho d")]
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("M ethod")]
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(Elem entName = "MethodName ")]
public string MethodName;
/// <summary>
/// The parameters to be sent to the method. Can be null.
/// </summary>
[XmlArrayItem("P arameter")]
public Object[] Parameters;
/// <summary>
/// The expected result object.
/// </summary>
[XmlElement(Elem entName = "Result")]
public Object Result;
}
Here is the method I'm using to create the XmlAttributeOve rrides
object:
/// <summary>
/// Determines all the types to be included.
/// </summary>
/// <returns>Retu rn an array of types included in the
object.</returns>
private XmlAttributeOve rrides GetOverridesToB eSerialized()
{
// Let's find the assembly used by ExternalOrder.
System.Reflecti on.Assembly bizAssembly =
typeof(External Order).Assembly ;
// Get all the types in the assembly.
System.Type[] bizTypes = bizAssembly.Get Types();
ArrayList objectTypes = new ArrayList();
XmlAttributeOve rrides attrOverrides = new XmlAttributeOve rrides();
foreach (System.Type type in bizTypes)
{
// Skip interfaces.
if (!type.IsInterf ace)
{
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(MsgOrde r));
foreach (System.Type type in objectTypes)
{
XmlElementAttri bute attr = new XmlElementAttri bute(type.Name,
type);
// Create the XmlAttributes class.
XmlAttributes attrs = new XmlAttributes() ;
attrs.XmlElemen ts.Add(attr);
attrOverrides.A dd(type, type.Name, attrs);
}
return attrOverrides;
}