ExtremeFitness
asked on
VB.NET XML Serialization How to convert a collection to XML
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(mvarF ilename) Then Return False
Dim wri As New IO.StreamWriter(mvarFilena me)
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.x ml")
Try
Dim Ser As New Xml.Serialization.XmlSeria lizer(GetT ype(clsPer son))
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</Fir stName>
<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</LastNam e>
<PhoneNumber>(416) 516-4509</PhoneNumber>
<Selected>true</Selected>
-------------------------- ---------- ---------- ---------- ---------- ----
Can you show me the right way of doing this?
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(mvarF
Dim wri As New IO.StreamWriter(mvarFilena
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.x
Try
Dim Ser As New Xml.Serialization.XmlSeria
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</Fir
<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</LastNam
<PhoneNumber>(416) 516-4509</PhoneNumber>
<Selected>true</Selected>
--------------------------
Can you show me the right way of doing this?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.