473,666 Members | 2,258 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to XMLSerialize Collection Class

Hi,

I have a collection class where I've implemeneted the
ICollection Interface.

Here is a small code segment.

public class PageList : ICollection, IComparer,
IEnumerable, IList
{
protected ArrayList _pages;

public PageList()
{
_pages = new ArrayList();
}
#region IList Members
public object this[int index]
{
get
{
if (index > _pages.Count)
{
return (Page)null;
}
else
{
return (Page)_pages
[index];
}
}
set
{
}
}

public int Add(object objPage)
{

int arrayIndexAdded = -1;
arrayIndexAdded = _pages.Add
(objPage);
return arrayIndexAdded ;
}

}

I have another class which declares this PageList class
and creates an instance of it.

How do I serialized this class?
Nov 12 '05 #1
3 4263
Hman,

You should be able to serialize instances like this:

PageList pl = new PageList();

// populate pl

XmlSerializer ser = new XmlSerializer( typeof( PageList ), new Type[] {
... } ); // enumerate all the types in the list.
ser.Serialize( writer, pl );

be careful though ... if you don't know what the types in your List are,
then getting the Types from the list elements and instantiating a serializer
instance is a very expensive operation and if performance matters, then you
should probably alter your design to not go that route.

I also have a few questions about the PageList class. Why do you implement
everything by hand, is there any reason you don't derive from CollectionBase
or ? And why are you even implementing a custom List if you don't strongly
type it? You may just as well stick to the ArrayList.

--
HTH
Christoph Schittko [MVP, XmlInsider]
Software Architect, .NET Mentor
"hman" <an*******@disc ussions.microso ft.com> wrote in message
news:08******** *************** *****@phx.gbl.. .
Hi,

I have a collection class where I've implemeneted the
ICollection Interface.

Here is a small code segment.

public class PageList : ICollection, IComparer,
IEnumerable, IList
{
protected ArrayList _pages;

public PageList()
{
_pages = new ArrayList();
}
#region IList Members
public object this[int index]
{
get
{
if (index > _pages.Count)
{
return (Page)null;
}
else
{
return (Page)_pages
[index];
}
}
set
{
}
}

public int Add(object objPage)
{

int arrayIndexAdded = -1;
arrayIndexAdded = _pages.Add
(objPage);
return arrayIndexAdded ;
}

}

I have another class which declares this PageList class
and creates an instance of it.

How do I serialized this class?

Nov 12 '05 #2
Hi, thanks for your help.
Actually, I have another class (Survey) which has an
instance of my PageList class.

code segment:

public class Survey
{
//survey has a collection of pages
private PageList _pagesCol;
private Page _currentPage;

public Survey()
{
_pagesCol = new PageList();
Page defaultFirstPag e = new Page();
defaultFirstPag e.Name = "Page 1";
_pagesCol.Add (defaultFirstPa ge);
}

}

I am simply serializing an intance of Survey:

// Serialization
XmlSerializer s = new XmlSerializer(t ypeof(Survey));
TextWriter w = new StreamWriter(@" C:\SCSurvey.xml ");
s.Serialize(w, _survey);
w.Close();

where _survey is my Survey instance created elsewhere.
Why isn't my PageList intance serialized?
-
-I don't know what strongly types custom lists are.

-----Original Message-----
Hman,

You should be able to serialize instances like this:

PageList pl = new PageList();

// populate pl

XmlSerialize r ser = new XmlSerializer( typeof( PageList ), new Type[] { ... } ); // enumerate all the types in the list.
ser.Serializ e( writer, pl );

be careful though ... if you don't know what the types in your List are,then getting the Types from the list elements and instantiating a serializerinstance is a very expensive operation and if performance matters, then youshould probably alter your design to not go that route.

I also have a few questions about the PageList class. Why do you implementeverything by hand, is there any reason you don't derive from CollectionBaseor ? And why are you even implementing a custom List if you don't stronglytype it? You may just as well stick to the ArrayList.

--
HTH
Christoph Schittko [MVP, XmlInsider]
Software Architect, .NET Mentor
"hman" <an*******@disc ussions.microso ft.com> wrote in messagenews:08******* *************** ******@phx.gbl. ..
Hi,

I have a collection class where I've implemeneted the
ICollection Interface.

Here is a small code segment.

public class PageList : ICollection, IComparer,
IEnumerable, IList
{
protected ArrayList _pages;

public PageList()
{
_pages = new ArrayList();
}
#region IList Members
public object this[int index]
{
get
{
if (index > _pages.Count)
{
return (Page)null;
}
else
{
return (Page)_pages
[index];
}
}
set
{
}
}

public int Add(object objPage)
{

int arrayIndexAdded = -1;
arrayIndexAdded = _pages.Add
(objPage);
return arrayIndexAdded ;
}

}

I have another class which declares this PageList class
and creates an instance of it.

How do I serialized this class?

.

Nov 12 '05 #3

Got everything working. Thanks.
-----Original Message-----
Hi, thanks for your help.
Actually, I have another class (Survey) which has an
instance of my PageList class.

code segment:

public class Survey
{
//survey has a collection of pages
private PageList _pagesCol;
private Page _currentPage;

public Survey()
{
_pagesCol = new PageList();
Page defaultFirstPag e = new Page();
defaultFirstPag e.Name = "Page 1";
_pagesCol.Add (defaultFirstPa ge);
}

}

I am simply serializing an intance of Survey:

// Serialization
XmlSerialize r s = new XmlSerializer(t ypeof(Survey));
TextWriter w = new StreamWriter(@" C:\SCSurvey.xml ");
s.Serialize( w, _survey);
w.Close();

where _survey is my Survey instance created elsewhere.
Why isn't my PageList intance serialized?
-
-I don't know what strongly types custom lists are.

-----Original Message-----
Hman,

You should be able to serialize instances like this:

PageList pl = new PageList();

// populate pl

XmlSerializ er ser = new XmlSerializer( typeof(PageList ), new Type[] {
... } ); // enumerate all the types in the list.
ser.Serialize ( writer, pl );

be careful though ... if you don't know what the types

inyour List are,
then getting the Types from the list elements andinstantiatin g a serializer
instance is a very expensive operation and if

performancematters, then you
should probably alter your design to not go that route.

I also have a few questions about the PageList class.
Whydo you implement
everything by hand, is there any reason you don't derive

from CollectionBase
or ? And why are you even implementing a custom List if

you don't strongly
type it? You may just as well stick to the ArrayList.

--
HTH
Christoph Schittko [MVP, XmlInsider]
Software Architect, .NET Mentor
"hman" <an*******@disc ussions.microso ft.com> wrote in

message
news:08****** *************** *******@phx.gbl ...
Hi,

I have a collection class where I've implemeneted the
ICollection Interface.

Here is a small code segment.

public class PageList : ICollection, IComparer,
IEnumerable, IList
{
protected ArrayList _pages;

public PageList()
{
_pages = new ArrayList();
}
#region IList Members
public object this[int index]
{
get
{
if (index > _pages.Count)
{
return (Page)null;
}
else
{
return (Page)_pages
[index];
}
}
set
{
}
}

public int Add(object objPage)
{

int arrayIndexAdded = -1;
arrayIndexAdded = _pages.Add
(objPage);
return arrayIndexAdded ;
}

}

I have another class which declares this PageList class
and creates an instance of it.

How do I serialized this class?

.

.

Nov 12 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2527
by: Marc L'Ecuyer | last post by:
Hi, I have a collection class derived from CollectionBase. This collection can add items of my class MyItem. In my class MyItem, I have a Selected property. When this property is set to true, I have to set this property to false for all other items in the collection (only 1 item can be selected). How can I do that? Thanks
0
1925
by: Sundown | last post by:
I am trying to create a custom button control for the web that, when clicked, disables and changes the text of itself and a bunch of other controls (in the collection). My goal is to end up with a button that prevents the user from submitting a form back multiple times and also prevents them from submitting the form and then clicking something else before the postback can finish processing. What I have is a working button that disables...
2
2229
by: Peter Bates | last post by:
Hi, I'm just getting used to XSDObjectGen and i have the following question. Can i use a class inherited from a class generated by XSDObjectGen with XmlSerialize? Specifically, I have many xml files arriving from a PC inventory scanner we use. I wish to deserialize them and then process them.
2
2510
by: Edward Diener | last post by:
In C++ an overridden virtual function in a derived class must have the exact same signature of the function which is overridden in the base class, except for the return type which may return a pointer or reference to a derived type of the base class's return type. In .NET the overridden virtual function is similar, but an actual parameter of the function can be a derived reference from the base class's reference also. This dichotomy...
0
846
by: marfi95 | last post by:
I'm serializing a dataset out to an xmlfile on a common drive my users share. Do I need to be concerned about concurrent access or does the serialize method of the xmlserialize object handle it ? Should I be using a mutex ? Mark
4
2510
by: Michael | last post by:
Dear all .. If I want to use develop a user control and declare a public property which the type is System.Windows.Forms.GridTableStylesCollection For example : Public Class LookAndView Inherits System.Windows.Forms.UserControl Private _Collection As GridTableStylesCollection
19
4907
by: Jamey Shuemaker | last post by:
I'm in the process of expanding my knowledge and use of Class Modules. I've perused MSDN and this and other sites, and I'm pretty comfortable with my understanding of Class Modules with the exception of custom Collection Classes. Background: I'm developing an A2K .mdb to be deployed as an .mde at my current job-site. It has several custom controls which utilize custom classes to wrap built-in controls, and add additional functionality....
6
7205
by: Arthur Dent | last post by:
How do you sort a generic collection derived from System.Collections.ObjectModel.Collection? Thanks in advance, - Arthur Dent
11
4153
by: Joe | last post by:
I know I've seen this before but I can't remember how to serialize a class with it's definition. For example I want each class to serialize not only with it's fields' values but the fields data type as well. I'm using XmlSerializer now. Thanks, Joe
0
8866
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
8639
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
6192
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
5663
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4198
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
4366
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2769
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
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1772
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.