473,386 Members | 1,830 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,386 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 6978
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: SammyBar | last post by:
Hi all, I have some custom collections (ArrayList of custom Objects) and I want to display them using typical Windows form controls (DataGrid, Combo). What should I do to expose them as valid...
1
by: womber | last post by:
I am getting XML from a dataset that has been populated via a storedprocedure no schemas have been applied nor any relationships. But the correct table names have been given to match the table(s)...
0
by: Thomas D. | last post by:
Situation: --------------------------- I have an 'export'-wrapper to my regular objects. For each regular object there is also an export object. An export object derives from the regular object...
2
by: Sebastian Sylvan | last post by:
Hello. I'm trying to serialize an object using the XML-serializer. A simple object is okay, But when I try to serialize an object which has a public List<SomeType> the program hangs upon calling...
0
by: Adam J. Schaff | last post by:
Hello. I have a custom collection that implements IBindingList (allownew and allowremove are both true). I have bound it to a datagrid. I have add and remove buttons on the screen. I want to...
3
by: ECathell | last post by:
Collections are really kicking me hard. I have a class Box. It has 3 properties(Boxtype,BoxTare,BoxDescription) I also want to make a BoxCollection object. I have tried using collectionbase...
4
by: John Dalberg | last post by:
I noticed the starterkits timetracker & issue tracker load data from a database into custom collections (arraylists) which bind to a datagrid. What are the advantages of using custom collections...
3
by: Nick | last post by:
I am developing a new management system for my company, and with it have 3 layers of functionality written in VB.NET 2.0. In the business logic layer I have a user class which contains all user...
1
by: Suresh | last post by:
Using ASP.NET 1.1 ---------------------- I have a custom collection of object that I'm binding to a datagrid. Each of these objects have another collection inside them. I have a user control...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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
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...

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.