473,394 Members | 1,481 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Array Serialization

(Note I have posted to this group because I had no luck in
microsoft.public.dotnet.vb.general)

Hi all

This is probably an easy one... I would like to serialize an array to
an XML file but I cant quite get it right.

Here goes

I declare the array at the top of the class

Private marrApplications(100) As App

Then when I go to write the array...

Private Sub save_Applications()
'serialize the object
Dim oXS As XmlSerializer = New XmlSerializer(GetType(App))
Dim oStmW As StreamWriter
Dim i As Integer

Try
oStmW = New StreamWriter(Application.StartupPath.ToString&
"\\Applications.xml")
For i = 0 To m_applicationCount - 1
oXS.Serialize(oStmW, marrApplications(i))
Next
oStmW.Close()
Catch ex As Exception
Debug.Write(ex.Message)
End Try
End Sub

It sort of saves the array but it is not properly formed in XML. Here
is the result...

<?xml version="1.0" encoding="utf-8"?>
<Class_App xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Property_Name>Blah</Property_Name>
<Property_ConnectionString>Blah</Property_ConnectionString>
<Property_ConnectionCount>0</Property_ConnectionCount>
<Property_MaxConnections>100</Property_MaxConnections>
</Class_App><?xml version="1.0" encoding="utf-8"?>
<Class_App xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Property_Name>Blah</Property_Name>
<Property_ConnectionString>Blah</Property_ConnectionString>
<Property_ConnectionCount>0</Property_ConnectionCount>
<Property_MaxConnections>100</Property_MaxConnections>
</Class_App>

I know this isnt right but what am I doing wrong?

Any help will be much appreciated, thanks.
John.
Nov 12 '05 #1
1 5960
you are not serializing the array.
Your code is serializing N elements of the array, one at a time. Which is
why you get a stream with N xml documents, all concatenated together. The
concatenated result does not constitute a well formed xml doc, as you
pointed out.

Instead of serializing N array elements, serialize the entire array at once.
When you get the serializer, you should do
Dim oXS As XmlSerializer = New XmlSerializer(GetType(App[]))
(I am not a vb guy so I do not know if that is legal syntax for "GetType".
but you get the idea)

Then oXS.Serialize(oStmW, marrApplications)
( no iteration ! )

-Dino
"John Tear" <jo******@hotmail.com> wrote in message
news:92**************************@posting.google.c om... (Note I have posted to this group because I had no luck in
microsoft.public.dotnet.vb.general)

Hi all

This is probably an easy one... I would like to serialize an array to
an XML file but I cant quite get it right.

Here goes

I declare the array at the top of the class

Private marrApplications(100) As App

Then when I go to write the array...

Private Sub save_Applications()
'serialize the object
Dim oXS As XmlSerializer = New XmlSerializer(GetType(App))
Dim oStmW As StreamWriter
Dim i As Integer

Try
oStmW = New StreamWriter(Application.StartupPath.ToString&
"\\Applications.xml")
For i = 0 To m_applicationCount - 1
oXS.Serialize(oStmW, marrApplications(i))
Next
oStmW.Close()
Catch ex As Exception
Debug.Write(ex.Message)
End Try
End Sub

It sort of saves the array but it is not properly formed in XML. Here
is the result...

<?xml version="1.0" encoding="utf-8"?>
<Class_App xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Property_Name>Blah</Property_Name>
<Property_ConnectionString>Blah</Property_ConnectionString>
<Property_ConnectionCount>0</Property_ConnectionCount>
<Property_MaxConnections>100</Property_MaxConnections>
</Class_App><?xml version="1.0" encoding="utf-8"?>
<Class_App xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Property_Name>Blah</Property_Name>
<Property_ConnectionString>Blah</Property_ConnectionString>
<Property_ConnectionCount>0</Property_ConnectionCount>
<Property_MaxConnections>100</Property_MaxConnections>
</Class_App>

I know this isnt right but what am I doing wrong?

Any help will be much appreciated, thanks.
John.

Nov 12 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Buddy Ackerman | last post by:
I created a simple class: Public Class MyTestClass Public Test() As String End Class I tried to assign some values to the array Test() and display them like this:
3
by: Franz | last post by:
Let me describe the flow of my program first. 1. Deserialize data from xml file. 2. Addition of "PersonType" class to the AllPersonalData. 3. Serialize data back to the xml file. My question is...
5
by: Abraham Lopez | last post by:
Hi.. Is there a way to convert a System.Array to XML... If you know thanks very much... if you don't... Please do not respond stupid things like " Yes -- many ways."
1
by: elziko | last post by:
My intention is to store an array of singles inside a DataTable so that it can me peristed somehow, maybe XML file, maybe Access/SQL Server I don't know yet so I'm just saving it as an XML file for...
1
by: parrot toes | last post by:
I tried to post this question before, but there was an error when posting. I case it did get posted and in order to avoid duplication, I'll just repost a summary. I have written a dotnet client...
0
by: Mrozik | last post by:
after parsing WSDL definition of java WebService, C# proxy class contains data strutures (I'm using RPC\encoded): public class DateString { /// <remarks/> public string value; }
5
by: Stacey Levine | last post by:
I have a webservice that I wanted to return an ArrayList..Well the service compiles and runs when I have the output defined as ArrayList, but the WSDL defines the output as an Object so I was...
2
by: tiberiu.motoc | last post by:
Hi. I've asked this question on the MSDN forums, but I've had no replies. I'm really stuck, so I'm gonna try my luck here. I have a Java web service which contains a simple function; the...
6
by: Glenn | last post by:
OK, need help in translating what I'd like to do in old school C to best method C# If I was wanting to create an array of struct's, writing and reading them to/from a file.. how would I do this...
5
by: =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post by:
Hi Willy, Thank you very much for your work. C++ code doesnot make any serialization. So at runtime C# code gives an serialization error at "msg_file_s sa = (msg_file_s) bf.Deserialize(ms);"...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.