473,654 Members | 3,272 Online
Bytes | Software Development & Data Engineering Community
+ 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 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;
}
Nov 16 '05 #1
0 1983

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

Similar topics

37
4986
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 signature. When developing this lib, I figured that the pointer-to-member-function, although seemingly an attractive solution, does not work well for us.
4
3130
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 data objects as XML but perserve the relationships in the XML document. For example, if my classes were: public class GenericItem { }
0
289
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 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...
6
4150
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 remove fields from the class. I've been able to deserialize out-of-date versions by handling serialization exceptions in Sub New(ByVal info As SerializationInfo, ByVal context As StreamingContext). I've now hit a problem. With the latest version, if...
1
3096
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
1705
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 signature has changed so I added: Protected Sub New(ByVal info As SerializationInfo, ByVal context As StreamingContext) End Sub to my new class so that I can specify custom deserialization however it does
9
2064
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 if I try to deserialize with another different app, I get an exception (the assembly RegionViewer is the app that serialized the
4
1679
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 causes big performance problem with the application. I learnt that it is Serializing/Deserializing causing the system to be slow. Serializing is required because I am storing the ViewState to the SessionState and SessionState is in...
0
2476
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 serializable classes (Class2 and Class3) Class1, Class2 and Class3 are defined in an assembly (Assembly1) which is loaded by another assembly (Assembly2). When I statically reference Assembly1 in Assembly2, serialization and deserialization...
0
8379
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8709
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8494
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8596
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7309
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6162
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5627
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2719
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 we have to send another system
2
1597
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.