?
Solved

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

Posted on 2006-11-29
1
Medium Priority
?
1,471 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 2

Accepted Solution

by:
AmNoGeek earned 2000 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying 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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses
Course of the Month11 days, 12 hours left to enroll

752 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