469,352 Members | 2,150 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,352 developers. It's quick & easy.

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 12117
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Michel | last post: by
1 post views Thread by nospam | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.