471,075 Members | 986 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

XmlSerializer and Custom Collections

Hi

I've been reading using the XmlSerializer with custom collections.

I've discovered that when serializing a custom collection (a class that
implements ICollection, IList etc.) the XmlSerializer will only serialize
the collection items - with the default root as ArrayofMyItems etc.

My custom collection class has some additional public properties that I
would like to include in the serialization above the items element array (in
my own root) - but if I've understood this correctly XmlSerializer will only
serialize a collection via the collection interface - ignoring all other
public collection properties. I understand the reason for this - it
considers these as auxilary - and that they should be recreated when the
class is deserialized.

In my case I have a couple of special integer properties that include
statistical information about the collection - i.e. data that was included
when the collection was originally created - but can not be recreated during
deserialiation of the collection items only.

I want the format of the XmlSerializer - and not the SOAP formater (which I
could use if I implement ISerializable - adding my properties to the object
graph).

Any suggestions? Or will I simply have to write my own Xml document helper
method to produce the Xml version of this class?

Any tips or advice greatly appreciated,

Tony
Nov 12 '05 #1
3 6827
Hello

i think that i have the same problem as you and i resolve it like this way

this is the serializeble clas

<Serializable()> _
Public Class DIF_File
Public VERSION As VERSION
<XmlArray()> _
Public DATA() As ImageProperty
End Class

<Serializable()> _
Public Class ImageProperty
<XmlAttributeAttribute(DataType:="string", AttributeName:="DocKey")>
Public DocKey As String
<XmlAttributeAttribute(DataType:="string", AttributeName:="AddressKey")>
Public AddressKey As String
End Class

As you can see i dont have any collection, but arrays.
in you code to enter de data in this class you have to create instance of
arrays with the lenght you want.

hope this help you.

(())
Paulo Aboim Pinto
Portugal

"Anthony Bouch" <to**@nospamforever.com> wrote in message
news:uU**************@TK2MSFTNGP09.phx.gbl...
Hi

I've been reading using the XmlSerializer with custom collections.

I've discovered that when serializing a custom collection (a class that
implements ICollection, IList etc.) the XmlSerializer will only serialize
the collection items - with the default root as ArrayofMyItems etc.

My custom collection class has some additional public properties that I
would like to include in the serialization above the items element array (in
my own root) - but if I've understood this correctly XmlSerializer will only
serialize a collection via the collection interface - ignoring all other
public collection properties. I understand the reason for this - it
considers these as auxilary - and that they should be recreated when the
class is deserialized.

In my case I have a couple of special integer properties that include
statistical information about the collection - i.e. data that was included
when the collection was originally created - but can not be recreated during
deserialiation of the collection items only.

I want the format of the XmlSerializer - and not the SOAP formater (which I
could use if I implement ISerializable - adding my properties to the object
graph).

Any suggestions? Or will I simply have to write my own Xml document helper
method to produce the Xml version of this class?

Any tips or advice greatly appreciated,

Tony

Nov 12 '05 #2
Hi Anthony,

IXmlSerializable is an interface that is recognized by the XmlSerializer. It
is pretty simple to understand how it works. It is not a published interface
so be aware it may change in future versions of the framework.

You can still use the XmlSerializer for your collection, just add some code
to store your extra properties. Here is an example:
void IXmlSerializable.WriteXml(XmlWriter writer)
{
writer.WriteElementString("MyPropery1", m_Property1);
writer.WriteElementString("MyPropery2", m_Property2);

XmlSerializer serializer = new XmlSerializer(typeof(MyCollection));
serializer.Serialize(writer, m_MyCollection);

}

void IXmlSerializable.ReadXml(XmlReader reader)
{
// Keep a strict element order if you are using ReadElementString
m_Property1 = reader.ReadElementString("MyPropery1");
m_Property2 = reader.ReadElementString("MyPropery2");

XmlSerializer serializer = new XmlSerializer(typeof(MyCollection));
m_MyCollection= (MyCollection)serializer.Deserialize(reader);
}
--
Ross Donald
Rad Software
Free Regular Expression Designer @
http://www.radsoftware.com.au/web/Products/
"Anthony Bouch" <to**@nospamforever.com> wrote in message
news:uU**************@TK2MSFTNGP09.phx.gbl...
| Hi
|
| I've been reading using the XmlSerializer with custom collections.
|
| I've discovered that when serializing a custom collection (a class that
| implements ICollection, IList etc.) the XmlSerializer will only serialize
| the collection items - with the default root as ArrayofMyItems etc.
|
| My custom collection class has some additional public properties that I
| would like to include in the serialization above the items element array
(in
| my own root) - but if I've understood this correctly XmlSerializer will
only
| serialize a collection via the collection interface - ignoring all other
| public collection properties. I understand the reason for this - it
| considers these as auxilary - and that they should be recreated when the
| class is deserialized.
|
| In my case I have a couple of special integer properties that include
| statistical information about the collection - i.e. data that was included
| when the collection was originally created - but can not be recreated
during
| deserialiation of the collection items only.
|
| I want the format of the XmlSerializer - and not the SOAP formater (which
I
| could use if I implement ISerializable - adding my properties to the
object
| graph).
|
| Any suggestions? Or will I simply have to write my own Xml document helper
| method to produce the Xml version of this class?
|
| Any tips or advice greatly appreciated,
|
| Tony
|
|
Nov 12 '05 #3
Hi Anthony,

IXmlSerializable is an interface that is recognized by the XmlSerializer. It
is pretty simple to understand how it works. It is not a published interface
so be aware it may change in future versions of the framework.

You can still use the XmlSerializer for your collection, just add some code
to store your extra properties. Here is an example:
void IXmlSerializable.WriteXml(XmlWriter writer)
{
writer.WriteElementString("MyPropery1", m_Property1);
writer.WriteElementString("MyPropery2", m_Property2);

XmlSerializer serializer = new XmlSerializer(typeof(MyCollection));
serializer.Serialize(writer, m_MyCollection);

}

void IXmlSerializable.ReadXml(XmlReader reader)
{
// Keep a strict element order if you are using ReadElementString
m_Property1 = reader.ReadElementString("MyPropery1");
m_Property2 = reader.ReadElementString("MyPropery2");

XmlSerializer serializer = new XmlSerializer(typeof(MyCollection));
m_MyCollection= (MyCollection)serializer.Deserialize(reader);
}
--
Ross Donald
Rad Software
Free Regular Expression Designer @
http://www.radsoftware.com.au/web/Products/
"Anthony Bouch" <to**@nospamforever.com> wrote in message
news:uU**************@TK2MSFTNGP09.phx.gbl...
| Hi
|
| I've been reading using the XmlSerializer with custom collections.
|
| I've discovered that when serializing a custom collection (a class that
| implements ICollection, IList etc.) the XmlSerializer will only serialize
| the collection items - with the default root as ArrayofMyItems etc.
|
| My custom collection class has some additional public properties that I
| would like to include in the serialization above the items element array
(in
| my own root) - but if I've understood this correctly XmlSerializer will
only
| serialize a collection via the collection interface - ignoring all other
| public collection properties. I understand the reason for this - it
| considers these as auxilary - and that they should be recreated when the
| class is deserialized.
|
| In my case I have a couple of special integer properties that include
| statistical information about the collection - i.e. data that was included
| when the collection was originally created - but can not be recreated
during
| deserialiation of the collection items only.
|
| I want the format of the XmlSerializer - and not the SOAP formater (which
I
| could use if I implement ISerializable - adding my properties to the
object
| graph).
|
| Any suggestions? Or will I simply have to write my own Xml document helper
| method to produce the Xml version of this class?
|
| Any tips or advice greatly appreciated,
|
| Tony
|
|
Nov 12 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Thomas D. | last post: by
2 posts views Thread by Sebastian Sylvan | last post: by
3 posts views Thread by ECathell | last post: by
3 posts views Thread by Nick | last post: by
1 post views Thread by Suresh | last post: by
reply views Thread by leo001 | last post: by

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.