Solved

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

Posted on 2006-11-29
1
1,440 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
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…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

861 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

29 Experts available now in Live!

Get 1:1 Help Now