Solved

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

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

791 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