473,396 Members | 2,038 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Overridden XML serialization is deserializing incorrectly.

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;
}
Nov 16 '05 #1
0 1933

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
4
by: Jeff T. | last post by:
Hello, I have an existing set of C# classes that encapsulate our application data. They are in a heirachy with each subclass defining more specific types of data. I would like to serialize these...
0
by: Casey | last post by:
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...
6
by: Tim Anderson | last post by:
I have an app that uses the SOAP serializer to serialize and deserialize a object of a certain class to a file. The class implements ISerializable. From time to time it is necessary to add or...
1
by: Sek | last post by:
Hi Folks, While deserialization in C#, i came across this error: "No assembly ID for object type <MyClassName>". The error is very abstract. Pls find below the stack trace. StackTrace at
5
by: Perecli Manole | last post by:
I have a class that has been serialized and saved to disk. I am trying to deserialize it back into the same class which now has an extra private member. It will not deserialize because its...
9
by: norvinl | last post by:
Hi, I'm serializing a class and using shared memory / deserialization to send it to other processes. I can serialize with one app and deserialize with another instance of the same app. But...
4
by: anandsoni | last post by:
I have been working with a project where I need to deal with lot of data. This data includes 10 thousands of records in the data grid. There are thousand of users connected to the application, this...
0
by: =?Utf-8?B?Y211cmFsaQ==?= | last post by:
I am serializing/deserializing a class (Class1) using the XmlSerializer object in the System.Xml.Serialization namespace in .Net 2.0. Class1 has some strings and ints and two lists of other simple...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.