473,626 Members | 3,443 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6992
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
<XmlAttributeAt tribute(DataTyp e:="string", AttributeName:= "DocKey")>
Public DocKey As String
<XmlAttributeAt tribute(DataTyp e:="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**@nospamfor ever.com> wrote in message
news:uU******** ******@TK2MSFTN GP09.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,

IXmlSerializabl e 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 IXmlSerializabl e.WriteXml(XmlW riter writer)
{
writer.WriteEle mentString("MyP ropery1", m_Property1);
writer.WriteEle mentString("MyP ropery2", m_Property2);

XmlSerializer serializer = new XmlSerializer(t ypeof(MyCollect ion));
serializer.Seri alize(writer, m_MyCollection) ;

}

void IXmlSerializabl e.ReadXml(XmlRe ader reader)
{
// Keep a strict element order if you are using ReadElementStri ng
m_Property1 = reader.ReadElem entString("MyPr opery1");
m_Property2 = reader.ReadElem entString("MyPr opery2");

XmlSerializer serializer = new XmlSerializer(t ypeof(MyCollect ion));
m_MyCollection= (MyCollection)s erializer.Deser ialize(reader);
}
--
Ross Donald
Rad Software
Free Regular Expression Designer @
http://www.radsoftware.com.au/web/Products/
"Anthony Bouch" <to**@nospamfor ever.com> wrote in message
news:uU******** ******@TK2MSFTN GP09.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,

IXmlSerializabl e 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 IXmlSerializabl e.WriteXml(XmlW riter writer)
{
writer.WriteEle mentString("MyP ropery1", m_Property1);
writer.WriteEle mentString("MyP ropery2", m_Property2);

XmlSerializer serializer = new XmlSerializer(t ypeof(MyCollect ion));
serializer.Seri alize(writer, m_MyCollection) ;

}

void IXmlSerializabl e.ReadXml(XmlRe ader reader)
{
// Keep a strict element order if you are using ReadElementStri ng
m_Property1 = reader.ReadElem entString("MyPr opery1");
m_Property2 = reader.ReadElem entString("MyPr opery2");

XmlSerializer serializer = new XmlSerializer(t ypeof(MyCollect ion));
m_MyCollection= (MyCollection)s erializer.Deser ialize(reader);
}
--
Ross Donald
Rad Software
Free Regular Expression Designer @
http://www.radsoftware.com.au/web/Products/
"Anthony Bouch" <to**@nospamfor ever.com> wrote in message
news:uU******** ******@TK2MSFTN GP09.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
324
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 datasources to these controls? What if I want that these collections work with Visual Studio on design time in order to allow me easy create grid columns on design time? Can you point me to some examples? Thanks in advance
1
1639
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) equivalent business object(s). I have a custom collection that implements ICollection and IEnumerable. Here is the collection: Imports ERP.BusinessObjects Imports ERP.BusinessObjects.Helpdesk Imports ICTObjects.Collections
0
1887
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 and has for each property in the base object an extra boolean property. That extra properties tells wether to include the base property in the final export (= webservice) or not. That is because not all the properties may be exposed all the time...
2
2356
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 the XmlSerializer constructor. I have verified that everything in the object is okay (not null). I've tried to give the list a type of IEnumerable instead of List<T> but that didn't work either. I'm using the Visual C# Express Beta.
0
2991
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 implement those buttons, but I'm not sure what the code should look like. I tried what I thought was obvious code: myCollection.AddNew() and
3
296
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 and arraylist, but I can't figure out how to "make it work" the way I think it should. Public Class BoxCollection Inherits ArrayList
4
2146
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 over simpler objects like datareaders or datatables? John Dalberg
3
1881
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 information. I also would like to put in a userCollection class for all my users. There are many other classes like this on as well. However, what is the best way to do my collections? I would like them so that a userCollection can only contain...
1
1917
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 that renders the nested collection into html table. See sample code below. Now this binds fine and my sub collections are rendered fine by my user control and list appears as it should. The problem is when I do paging. The sub collections do...
0
8268
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8202
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8707
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8510
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6125
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4093
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4202
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2628
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1812
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.