473,242 Members | 1,662 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,242 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 6965
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
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.