Deserialize Serialize this example:

P

Peter

How would would you deserialize this example below?

Imports System
Imports System.Collections
Imports System.IO
Imports System.Xml.Serialization

Public Class App1

Shared Sub Main()
Dim list As ArrayList = New ArrayList()
Dim out As StreamWriter = New StreamWriter("array.xml")
Dim ser As XmlSerializer = New XMLSerializer(list.GetType())

list.Add("a")
list.Add("2")
list.Add("three")

ser.Serialize(out, list)
End Sub
End Class
 
C

Cor Ligthert

Peter,

I think that you are confusing us. What you are trying to do is not
serializing in my opinion.
Shared Sub Main()
Dim list As ArrayList = New ArrayList()
Dim out As StreamWriter = New StreamWriter("array.xml")
Dim ser As XmlSerializer = New XMLSerializer(list.GetType())

list.Add("a")
list.Add("2")
list.Add("three")

ser.Serialize(out, list)

This seems if you want to convert a kind of XML document to an arraylist.
That is not serializing. That is using the XMLNodeReader. See this sample
that I once made, just as a start because it needs a lot more.

\\\\
Dim xmlString As String = "<department>" & _
"<employee name=""ABC"" age=""31"" sex=""male""/>" & _
"<employee name=""CDE"" age=""40"" sex=""male""/></department>"
Dim sr As New System.IO.StringReader(xmlString)
Dim doc As New Xml.XmlDocument
doc.Load(sr)
'or just in this case doc.LoadXML(xmlString)
Dim reader As New Xml.XmlNodeReader(doc)
While reader.Read()
Select Case reader.NodeType
Case Xml.XmlNodeType.Element
If reader.Name = "employee" Then
MessageBox.Show(reader.GetAttribute("name"))
End If
End Select
End While
///

I hope this helps?

Cor
 
H

Herfried K. Wagner [MVP]

Cor Ligthert said:
I think that you are confusing us. What you are trying to do is not
serializing in my opinion.


This seems if you want to convert a kind of XML document to an arraylist.
That is not serializing.

Huh?! The OP simply wants to deserialize an arraylist that has been
serialized to XML.
 
J

Jay B. Harlow [MVP - Outlook]

Cor
I think that you are confusing us. What you are trying to do is not
serializing in my opinion.
Huh????

You may want to review MSDN on what the XmlSerializer in .NET is!

http://msdn.microsoft.com/library/d...emxmlserializationxmlserializerclasstopic.asp

<quote>
XmlSerializer

Serializes and deserializes objects into and from XML documents.
</quote>

To Deserialize an object that was serialized with XmlSerializer you simply
call, wait for it, the Deserialize method!

Dim input As StreamReader
list = ser.Deserialze(input)

For a more complete example see my other post.

Hope this helps
Jay
 
J

Jay B. Harlow [MVP - Outlook]

Peter,
If you use XMLSerializer.Serialize to serialize an object, you can use
XMLSerializer.Deserialize to deserialize the object.

Something like:

Dim ser As XmlSerializer = New XMLSerializer(GetType(ArrayList))
Dim input As StreamReader = New StreamReader("array.xml")
Dim list As ArrayList = DirectCast(ser.Deserialize(input),
ArrayList)
input.Close()

For information on the XMLSerializer class with examples, see:

http://msdn.microsoft.com/library/d...ide/html/cpconintroducingxmlserialization.asp

http://msdn.microsoft.com/library/d...emxmlserializationxmlserializerclasstopic.asp

Hope this helps
Jay
 
C

Cor Ligthert

Jay,

I had changed my mind already and had reviewed these articles therefore
"again" on saterday.

And tried some things with using the arraylist with that, however I have not
the idea it is easy to create nodes from the objects from an arraylist, and
therefore I waited on a more concrete answer from Peter.

As well do I think it is much easier only to serialize an arraylist and not
try to bring it to an XML document. However that is as well waiting for an
answer from Peter.

Thanks any way for sending the links you could not know that.

Cor
 
H

Herfried K. Wagner [MVP]

Cor Ligthert said:
I had changed my mind already and had reviewed these articles therefore
"again" on saterday.

And tried some things with using the arraylist with that, however I have
not the idea it is easy to create nodes from the objects from an
arraylist, and therefore I waited on a more concrete answer from Peter.

As well do I think it is much easier only to serialize an arraylist and
not try to bring it to an XML document. However that is as well waiting
for an answer from Peter.

Thanks any way for sending the links you could not know that.

Huh (again)?!

When using XML serialization to serialize the arraylist, you don't need to
deal with 'XMLDocument' and nodes at all. The serializer/deserializer will
do that for you.
 
C

Cor Ligthert

Herfried K.Wagner
Huh (again)?!

When using XML serialization to serialize the arraylist, you don't need to
deal with 'XMLDocument' and nodes at all. The serializer/deserializer
will do that for you.

Again, show than that sample (when it is so simple as you repeatedly say)
when as the OP showed an from a class instanced object (or even different
objects, what he did not show) are the objects that the arraylist contains

Cor
 
H

Herfried K. Wagner [MVP]

Cor,

Cor Ligthert said:
Again, show than that sample (when it is so simple as you repeatedly say)
when as the OP showed an from a class instanced object (or even different
objects, what he did not show) are the objects that the arraylist contains

Jay already gave parts of an example and lots of related information.
 
H

Herfried K. Wagner [MVP]

Cor Ligthert said:
Yes that I wrote already long before you.

Then why do you expect that I post a sample? The question was/is already
answered by Jay B. Harlow [MVP - Outlook]. No need to repeat what he wrote.
 
C

Cor Ligthert

Herfried,

In contradiction too you, I did not repeat it, I was answering Jay and in
that was the conformation about what he wrote.

Although I readed his sample in his answer just as a link to the answer. To
do deserialization, you you have first have to serialize an arraylist, and
that is in my opinion not as simple as the answer from Jay.

Cor

"Herfried K. Wagner [MVP]"
Cor Ligthert said:
Yes that I wrote already long before you.

Then why do you expect that I post a sample? The question was/is already
answered by Jay B. Harlow [MVP - Outlook]. No need to repeat what he
wrote.
 
P

peter

The only thing I'm trying to accomplish here is to Export this
Arraylisr into a xml file and then re-import it back into another
arraylist.
I'll read through the posted articles tonight. Thank you.

-Peter
 
C

Cor Ligthert

Peter,

And when you do not succeed, than convert it first to a datatable, add that
to a dataset and than do ds.writexml(path) and to get it back
ds.readxml(path).

One of the reasons to use it as XML is why there are datasets you know.

Just my thought,

Cor
 
B

Brian

It is VERY easty to serialize/deserialize an arraylist, as long as the
contained objects in the arraylist are also serializable:
//DESERIALIZE
objFileStream = new FileStream( "ServerList.data", FileMode.Open );
_Servers = (ArrayList)objBinaryFormatter.Deserialize( objFileStream );
objFileStream.Close();
foreach(clsServer cServer in _Servers)
{
TreeNode tvNode = new TreeNode();
tvNode.Text = cServer.ServerName;
tvNode.Tag = "SERVER";
ctrlTree.Nodes[rNode.Index].Nodes.Add(tvNode);
}
//SERIALIZE
FileStream objFileStream;
BinaryFormatter objBinaryFormatter;
objFileStream = new FileStream( "ServerList.data" ,
FileMode.OpenOrCreate );
objBinaryFormatter = new BinaryFormatter();
objBinaryFormatter.Serialize(objFileStream, _Servers);
objFileStream.Close();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top