473,396 Members | 1,853 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.

How to use XmlAttributeOverrides

Hi there,

I'm trying to use XmlSerializer with overrides to generate xml for a
ArrayList that contains objects of a certain type (let's say class A). Class
A serializes fine, this is not an issue.
The following works fine (the array has an element name ArrayOfAnyType, the
containing elements are called anyType):

XmlSerializer x = new XmlSerializer(typeof(ArrayList),
myTypeArrayContainingA);

Now I'm trying XmlAttributeOverrides, so I can control names etc:

XmlAttributeOverrides overrides = new XmlAttributeOverrides();
XmlAttributes root = new XmlAttributes();
root.XmlRoot = new XmlRootAttribute("As");
root.XmlArrayItems.Add(new XmlArrayItemAttribute("A", typeof(A)));
overrides.Add(myArray.GetType(), root);
XmlSerializer x = new XmlSerializer(myArray.GetType(), overrides);

Creating the serializer fails with the exception:

An unhandled exception of type 'System.InvalidOperationException' occurred
in system.xml.dll
Additional information: There was an error reflecting type
'System.Collections.ArrayList'.

I've tried some other constructs, like adding XmlElementAttribute and such,
but I'm starting to get the feeling I really don't understand the mechanism.
Using the attributes as attributes has not given me any problems.

Any help is greatly appreciated,
Thanks,
Michel
Nov 12 '05 #1
4 12366
Michel,

Try [0] and [1] ...

Let me know if they help.

--
HTH
Christoph Schittko [MVP, XmlInsider]
Software Architect, .NET Mentor

[0] http://www.topxml.com/xmlserializer/...tomization.asp
[1] http://www.topxml.com/xmlserializer/..._overrides.asp

"Michel" <mi****@nospam.nl> wrote in message
news:c0*********@reader08.wxs.nl...
Hi there,

I'm trying to use XmlSerializer with overrides to generate xml for a
ArrayList that contains objects of a certain type (let's say class A). Class A serializes fine, this is not an issue.
The following works fine (the array has an element name ArrayOfAnyType, the containing elements are called anyType):

XmlSerializer x = new XmlSerializer(typeof(ArrayList),
myTypeArrayContainingA);

Now I'm trying XmlAttributeOverrides, so I can control names etc:

XmlAttributeOverrides overrides = new XmlAttributeOverrides();
XmlAttributes root = new XmlAttributes();
root.XmlRoot = new XmlRootAttribute("As");
root.XmlArrayItems.Add(new XmlArrayItemAttribute("A", typeof(A)));
overrides.Add(myArray.GetType(), root);
XmlSerializer x = new XmlSerializer(myArray.GetType(), overrides);

Creating the serializer fails with the exception:

An unhandled exception of type 'System.InvalidOperationException' occurred
in system.xml.dll
Additional information: There was an error reflecting type
'System.Collections.ArrayList'.

I've tried some other constructs, like adding XmlElementAttribute and such, but I'm starting to get the feeling I really don't understand the mechanism. Using the attributes as attributes has not given me any problems.

Any help is greatly appreciated,
Thanks,
Michel

Nov 12 '05 #2
Hi Christoph,

I'm starting to get the feeling you are my personal .net support line ;-)

Thanks for the links. I've retried with a fresh head (so to speak) but sofar
I can't get it to work.

I am serializing an ArrayList with objects of one type. I've tried the same
for a "normal" array. What I want is to change the names of the root (the
arraylist) and possibly it's items.

If I set XmlArray or XmlRoot I get:
XmlRoot and XmlType attributes may not be specified for the type

If I add an object to XmlArrayItems I get:
XML attributes may not be specified for the type
System.Collections.ArrayList.

B.t.w., on the link about runtime customization, it states:
"the XmlSerializer does not support dynamically adding an XmlArrayItem
attribute to arrays or collections at the root of the serialized object
graph."

Any ideas?
Thanks again,
Michel

"Christoph Schittko [MVP]" <ch********************@austin.rr.com> wrote in
message news:u1*************@TK2MSFTNGP12.phx.gbl...
Michel,

Try [0] and [1] ...

Let me know if they help.

--
HTH
Christoph Schittko [MVP, XmlInsider]
Software Architect, .NET Mentor

[0] http://www.topxml.com/xmlserializer/...tomization.asp
[1] http://www.topxml.com/xmlserializer/..._overrides.asp

"Michel" <mi****@nospam.nl> wrote in message
news:c0*********@reader08.wxs.nl...
Hi there,

I'm trying to use XmlSerializer with overrides to generate xml for a
ArrayList that contains objects of a certain type (let's say class A).

Class
A serializes fine, this is not an issue.
The following works fine (the array has an element name ArrayOfAnyType,

the
containing elements are called anyType):

XmlSerializer x = new XmlSerializer(typeof(ArrayList),
myTypeArrayContainingA);

Now I'm trying XmlAttributeOverrides, so I can control names etc:

XmlAttributeOverrides overrides = new XmlAttributeOverrides();
XmlAttributes root = new XmlAttributes();
root.XmlRoot = new XmlRootAttribute("As");
root.XmlArrayItems.Add(new XmlArrayItemAttribute("A", typeof(A)));
overrides.Add(myArray.GetType(), root);
XmlSerializer x = new XmlSerializer(myArray.GetType(), overrides);

Creating the serializer fails with the exception:

An unhandled exception of type 'System.InvalidOperationException' occurred in system.xml.dll
Additional information: There was an error reflecting type
'System.Collections.ArrayList'.

I've tried some other constructs, like adding XmlElementAttribute and

such,
but I'm starting to get the feeling I really don't understand the

mechanism.
Using the attributes as attributes has not given me any problems.

Any help is greatly appreciated,
Thanks,
Michel


Nov 12 '05 #3
Michel,

You can only change the name of the Array's root element by passing the
XmlRootAttribute to the XmlSerializer constructor:

XmlSerializer ser = new XmlSerializer( typeof(ArrayList), null, new Type[]
{ typeof(int) }, new XmlRootAttribute( "MyInts" ), null );

IIRC, there were some problems around changing the array item elements, but
I don't remember exactly what that was.
--
HTH
Christoph Schittko [MVP, XmlInsider]
Software Architect, .NET Mentor

"Michel" <mi****@nospam.nl> wrote in message
news:c0**********@reader11.wxs.nl...
Hi Christoph,

I'm starting to get the feeling you are my personal .net support line ;-)

Thanks for the links. I've retried with a fresh head (so to speak) but sofar I can't get it to work.

I am serializing an ArrayList with objects of one type. I've tried the same for a "normal" array. What I want is to change the names of the root (the
arraylist) and possibly it's items.

If I set XmlArray or XmlRoot I get:
XmlRoot and XmlType attributes may not be specified for the type

If I add an object to XmlArrayItems I get:
XML attributes may not be specified for the type
System.Collections.ArrayList.

B.t.w., on the link about runtime customization, it states:
"the XmlSerializer does not support dynamically adding an XmlArrayItem
attribute to arrays or collections at the root of the serialized object
graph."

Any ideas?
Thanks again,
Michel

"Christoph Schittko [MVP]" <ch********************@austin.rr.com> wrote in
message news:u1*************@TK2MSFTNGP12.phx.gbl...
Michel,

Try [0] and [1] ...

Let me know if they help.

--
HTH
Christoph Schittko [MVP, XmlInsider]
Software Architect, .NET Mentor

[0] http://www.topxml.com/xmlserializer/...tomization.asp
[1] http://www.topxml.com/xmlserializer/..._overrides.asp

"Michel" <mi****@nospam.nl> wrote in message
news:c0*********@reader08.wxs.nl...
Hi there,

I'm trying to use XmlSerializer with overrides to generate xml for a
ArrayList that contains objects of a certain type (let's say class A).

Class
A serializes fine, this is not an issue.
The following works fine (the array has an element name
ArrayOfAnyType,
the
containing elements are called anyType):

XmlSerializer x = new XmlSerializer(typeof(ArrayList),
myTypeArrayContainingA);

Now I'm trying XmlAttributeOverrides, so I can control names etc:

XmlAttributeOverrides overrides = new XmlAttributeOverrides();
XmlAttributes root = new XmlAttributes();
root.XmlRoot = new XmlRootAttribute("As");
root.XmlArrayItems.Add(new XmlArrayItemAttribute("A", typeof(A)));
overrides.Add(myArray.GetType(), root);
XmlSerializer x = new XmlSerializer(myArray.GetType(), overrides);

Creating the serializer fails with the exception:

An unhandled exception of type 'System.InvalidOperationException'

occurred in system.xml.dll
Additional information: There was an error reflecting type
'System.Collections.ArrayList'.

I've tried some other constructs, like adding XmlElementAttribute and

such,
but I'm starting to get the feeling I really don't understand the

mechanism.
Using the attributes as attributes has not given me any problems.

Any help is greatly appreciated,
Thanks,
Michel



Nov 12 '05 #4
Hi Christoph,

Your code example works fine, thanks.
Still, elements have the default anyType name.
I guess you shouldn't use ArrayList as a root for the Serializer.

Thanks,
Michel

"Christoph Schittko [MVP]" <ch********************@austin.rr.com> wrote in
message news:%2****************@tk2msftngp13.phx.gbl...
Michel,

You can only change the name of the Array's root element by passing the
XmlRootAttribute to the XmlSerializer constructor:

XmlSerializer ser = new XmlSerializer( typeof(ArrayList), null, new Type[]
{ typeof(int) }, new XmlRootAttribute( "MyInts" ), null );

IIRC, there were some problems around changing the array item elements, but I don't remember exactly what that was.
--
HTH
Christoph Schittko [MVP, XmlInsider]
Software Architect, .NET Mentor

"Michel" <mi****@nospam.nl> wrote in message
news:c0**********@reader11.wxs.nl...
Hi Christoph,

I'm starting to get the feeling you are my personal .net support line ;-)

Thanks for the links. I've retried with a fresh head (so to speak) but

sofar
I can't get it to work.

I am serializing an ArrayList with objects of one type. I've tried the

same
for a "normal" array. What I want is to change the names of the root (the arraylist) and possibly it's items.

If I set XmlArray or XmlRoot I get:
XmlRoot and XmlType attributes may not be specified for the type

If I add an object to XmlArrayItems I get:
XML attributes may not be specified for the type
System.Collections.ArrayList.

B.t.w., on the link about runtime customization, it states:
"the XmlSerializer does not support dynamically adding an XmlArrayItem
attribute to arrays or collections at the root of the serialized object
graph."

Any ideas?
Thanks again,
Michel

"Christoph Schittko [MVP]" <ch********************@austin.rr.com> wrote in message news:u1*************@TK2MSFTNGP12.phx.gbl...
Michel,

Try [0] and [1] ...

Let me know if they help.

--
HTH
Christoph Schittko [MVP, XmlInsider]
Software Architect, .NET Mentor

[0] http://www.topxml.com/xmlserializer/...tomization.asp
[1] http://www.topxml.com/xmlserializer/..._overrides.asp

"Michel" <mi****@nospam.nl> wrote in message
news:c0*********@reader08.wxs.nl...
> Hi there,
>
> I'm trying to use XmlSerializer with overrides to generate xml for a
> ArrayList that contains objects of a certain type (let's say class A). Class
> A serializes fine, this is not an issue.
> The following works fine (the array has an element name

ArrayOfAnyType, the
> containing elements are called anyType):
>
> XmlSerializer x = new XmlSerializer(typeof(ArrayList),
> myTypeArrayContainingA);
>
> Now I'm trying XmlAttributeOverrides, so I can control names etc:
>
> XmlAttributeOverrides overrides = new XmlAttributeOverrides();
> XmlAttributes root = new XmlAttributes();
> root.XmlRoot = new XmlRootAttribute("As");
> root.XmlArrayItems.Add(new XmlArrayItemAttribute("A", typeof(A)));
> overrides.Add(myArray.GetType(), root);
> XmlSerializer x = new XmlSerializer(myArray.GetType(), overrides);
>
> Creating the serializer fails with the exception:
>
> An unhandled exception of type 'System.InvalidOperationException'

occurred
> in system.xml.dll
> Additional information: There was an error reflecting type
> 'System.Collections.ArrayList'.
>
> I've tried some other constructs, like adding XmlElementAttribute and such,
> but I'm starting to get the feeling I really don't understand the
mechanism.
> Using the attributes as attributes has not given me any problems.
>
> Any help is greatly appreciated,
> Thanks,
> Michel
>
>



Nov 12 '05 #5

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

Similar topics

4
by: Zion Zadik | last post by:
Dear all, I have a set of c# data classes which i need to fill their data from xml files. serialization looks to be the best way to accomplish this task. Since the data classes are compiled and...
5
by: Michel | last post by:
Hi there, What is the best way to serialize unknown data? I have a class that contains a list of parameter objects. The parameter has a value which can be a simple value of a complex class,...
4
by: Alexis | last post by:
Hello, Is there a way of telling the XmlSerializer to ignore all namespaces when deserializing. I'm using XmlAttributeOverrides, but I have to do it for every class the OutputObject usses. Is...
0
by: Ryan B | last post by:
Hi there, I've been searching the posts in this group and haven't been able to find anything that suitably solves my problem.. anyway... I am attempting to deserialize a class which I do not...
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...
1
by: nospam | last post by:
Hi, I have used the xsd tool to generate a class from a schema. I then use the XmlSerializer class to write an object to an XML file. So far so good. However attributes that are set to...
3
by: Kishore Gopalan | last post by:
Hi, I have the following class. Assembly: A.dll public class Customers { public Customer customer; } public class Customer { public string name;
0
by: Simon Gregory | last post by:
I'm trying to override the default elementnames in the (seemingly) simple case of serializing an array of GUIDs into XML. Here's the basic code I started with: Dim arrGuids(3) as Guid ...
0
by: harshal.borade | last post by:
I am using XmlSerializer with XmlAttributeOverrides. The XmlAttributeOverrides are used for properties which hold element/s derived from "AbstractMessage" type. ...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...
0
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,...

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.