By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,932 Members | 1,547 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,932 IT Pros & Developers. It's quick & easy.

Deserialize Serialize this example:

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
17 Replies


P: n/a
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
Nov 21 '05 #2

P: n/a
"Cor Ligthert" <no************@planet.nl> schrieb:
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.


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

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #3

P: n/a

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


Why do you than not give a "simple" sample for that

Cor
Nov 21 '05 #4

P: n/a
"Cor Ligthert" <no************@planet.nl> schrieb:
Huh?! The OP simply wants to deserialize an arraylist that has been
serialized to XML.


Why do you than not give a "simple" sample for that


There is no guarantee that I'll answer a question.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #5

P: n/a
Herfried,
Huh?! The OP simply wants to deserialize an arraylist that has been
serialized to XML.
Why do you than not give a "simple" sample for that


There is no guarantee that I'll answer a question.


You said it was simple, so I could not resist that was the only reason.

:-)

Cor

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> "Cor Ligthert" <no************@planet.nl> schrieb:
--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #6

P: n/a
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/de...classtopic.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!
ser.Serialize(out, list)
Dim ser As XmlSerializer = New XMLSerializer(list.GetType())


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

For a more complete example see my other post.

Hope this helps
Jay

"Cor Ligthert" <no************@planet.nl> wrote in message
news:eC**************@tk2msftngp13.phx.gbl... 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

Nov 21 '05 #7

P: n/a
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/de...ialization.asp

http://msdn.microsoft.com/library/de...classtopic.asp

Hope this helps
Jay
"Peter" <pe***@mclinn.com> wrote in message
news:dc**************************@posting.google.c om...
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

Nov 21 '05 #8

P: n/a
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
Nov 21 '05 #9

P: n/a
"Cor Ligthert" <no************@planet.nl> schrieb:
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.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 21 '05 #10

P: n/a
>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
Nov 21 '05 #11

P: n/a
Cor,

"Cor Ligthert" <no************@planet.nl> schrieb:
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


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

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 21 '05 #12

P: n/a
Yes,

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

Yes that I wrote already long before you.

Cor
Nov 21 '05 #13

P: n/a
"Cor Ligthert" <no************@planet.nl> schrieb:
Jay already gave parts of an example and lots of related information.


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.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 21 '05 #14

P: n/a
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" <no************@planet.nl> schrieb:
Jay already gave parts of an example and lots of related information.


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.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 21 '05 #15

P: n/a
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

Nov 21 '05 #16

P: n/a
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

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.

Nov 21 '05 #17

P: n/a
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();
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 21 '05 #18

This discussion thread is closed

Replies have been disabled for this discussion.