Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / XML / November 2003

Tip: Looking for answers? Try searching our database.

How to XMLSerialize Collection Class

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
hman - 28 Nov 2003 23:38 GMT
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?
Christoph Schittko [MVP] - 29 Nov 2003 01:36 GMT
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.

Signature

HTH
Christoph Schittko [MVP, XmlInsider]
Software Architect, .NET Mentor

> Hi,
>
[quoted text clipped - 49 lines]
>
> How do I serialized this class?
- 29 Nov 2003 16:48 GMT
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 defaultFirstPage = new Page();
  defaultFirstPage.Name = "Page 1";
  _pagesCol.Add (defaultFirstPage);
 }

}

I am simply serializing an intance of Survey:

// Serialization
XmlSerializer s = new XmlSerializer(typeof(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,
[quoted text clipped - 74 lines]
>
>.
- 29 Nov 2003 20:36 GMT
Got everything working.  Thanks.

>-----Original Message-----
>Hi, thanks for your help.
[quoted text clipped - 123 lines]
>>
>.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.