Solved

VB.NET XML Serialization How to convert a collection to XML

Posted on 2006-11-29
1
1,435 Views
Last Modified: 2008-01-09
I have a collection of objects  'clsPerson' I want to serialize..
However when I do it the XML header is witten with each object instead of just at the top.

This is the code I use to serialize
    Public Function WriteXML(ByVal oCol As Collection) As Boolean
        If String.IsNullOrEmpty(mvarFilename) Then Return False

        Dim wri As New IO.StreamWriter(mvarFilename)
        For Each oObj As Object In oCol
            oObj.XMLSerialize(wri)
        Next
        wri.Close()
    End Function

This is in my 'clsPerson' Class
    Sub XMLSerialize(ByVal sWri As IO.StreamWriter) 'ByVal mc As clsPerson)
        'Dim wri As New IO.StreamWriter("Ppopcfg.xml")
        Try
            Dim Ser As New Xml.Serialization.XmlSerializer(GetType(clsPerson))
            Ser.Serialize(sWri, Me)
        Catch ex As Exception
            Stop
        End Try
    End Sub

The XML it produced. for 3 objects in the collection
----------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<clsPerson xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <FirstName>Sunnybrook</FirstName>
  <PhoneNumber>(416) 4804846 84144</PhoneNumber>
  <Selected>true</Selected>
</clsPerson><?xml version="1.0" encoding="utf-8"?>
<clsPerson xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <FirstName>St. Johns Rehab Hospitao</FirstName>
  <PhoneNumber>(416) 226-6780</PhoneNumber>
  <Selected>true</Selected>
</clsPerson><?xml version="1.0" encoding="utf-8"?>
<clsPerson xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <FirstName>Ken</FirstName>
  <LastName>Shelson</LastName>
  <PhoneNumber>(416) 516-4509</PhoneNumber>
  <Selected>true</Selected>
----------------------------------------------------------------------

Can you show me the right way of doing this?
0
Comment
Question by:ExtremeFitness
1 Comment
 
LVL 2

Accepted Solution

by:
AmNoGeek earned 500 total points
Comment Utility
Wouldn't you want to serialize your collection rather than serializing each object in the collection? That way it will create 3 clsPerson elements in the file.

Hope this link helps
http://www.codeproject.com/vb/net/escSerializedCollection.asp

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now