Re: Array Serialization

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Dino Chiesa [Microsoft] (dinoch_at_online.microsoft.com)
Date: 05/04/04


Date: Mon, 3 May 2004 23:09:53 -0400

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" <johntear@hotmail.com> wrote in message
news:92ae3244.0405021128.10dd8302@posting.google.com...
> (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.



Relevant Pages

  • Re: XMLSerializer, socket
    ... as an object array without having to worry about separating the data. ... send the length of the XML document you are going to send. ... the XMLs from each other in the stream of data. ... the XML telegrams and send them over the socket connection but when I ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Array to Xml to DataSet
    ... In order to read the contents of the stream into the DataSet you need to ... MS MVP XML ... Array to Xml to DataSet ... So I serialize the array to XML, ...
    (microsoft.public.dotnet.xml)
  • Re: Serialization performance
    ... I tried calling SetSizebefore serializing and then FreeExtra ... I didn't make a difference in the load time though. ... We're using an MFC typed ptr array, ...
    (microsoft.public.vc.mfc)
  • Re: Unserialize a single array element in a constant?
    ... that they must then be unserialized first before accessing them. ... I suppose, technically, if an array is first serialized before ... that the overhead of first serializing and then unserializing such a ...
    (comp.lang.php)
  • Re: Help with System.IO.Stream
    ... >XML is simply the format of those bytes. ... byteArr = encoding.GetBytes; ... move the data to a byte array, then use the byte array to instantiate ... a Stream object for passing to my Submitmethod. ...
    (microsoft.public.dotnet.languages.csharp)