Solved

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

Posted on 2006-11-29
1
1,444 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
ID: 18040906
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

809 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