473,480 Members | 1,852 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 802

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

Similar topics

37
4940
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
3116
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
1942
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
4133
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
3082
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
1696
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...
5
6000
by: Harold Howe | last post by:
I am having a problem deserializing objects from a library when the following conditions exist: 1- The library is strongly named 2- The serialized file was created with version 1.0 of the...
9
2052
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
1667
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
2456
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
7039
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6904
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
7037
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
7080
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
5326
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4770
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
2977
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1296
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
176
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.