Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML and serialize

Status
Not open for further replies.

mrdance

Programmer
Joined
Apr 17, 2001
Messages
308
Location
SE
I would like to create an XMLstring that looks like this:

<ONLINE>
<CHATTER>
<ID>1</ID>
<NICK>John</NICK>
</CHATTER>
<CHATTER>
<ID>2</ID>
<NICK>Mark</NICK>
</CHATTER>
</ONLINE>

Right now, I am only able to serialize it to this:

<ONLINE>
<CHATTER>
<ID>1</ID>
<NICK>John</NICK>
</CHATTER>
</ONLINE>

I don't know how to create a class so that I can serialize to more <CHATTER> objects. My code looks like this right now:

Public Class ONLINE
Private _chatter As New CHATTER
Public Property chatter() As chatter
Get
Return _chatter
End Get
Set(ByVal chatter As chatter)
_chatter = chatter
End Set
End Property
End Class
Public Class CHATTER
Private _id As String
Private _nick As String
Public Property id() As String
Get
Return _id
End Get
Set(ByVal id As String)
_id = id
End Set
End Property
Public Property nick() As String
Get
Return _nick
End Get
Set(ByVal nick As String)
_nick = nick
End Set
End Property
End Class

And the function like this:

Dim online As New ONLINE
online.chatter.id = &quot;1&quot;
online.chatter.nick = &quot;John&quot;
online.chatter.id = &quot;2&quot;
online.chatter.nick = &quot;Mark&quot;

xs_ONLINE.Serialize(sw, online)
Return sw.ToString

Maybe I am totally wrong with this, do you have any idea how to solve this? Thanks!


 
Create a class, for instance CHATTERS that holds a collection of CHATTER classes.

Add a function:
public function AddCHATTER(Byval vobjChatter As CHATTER)
... code to add to collection goes here.
end function

somewhere along these lines collections are being created....

Greetings,
Rick
 
Thanks, but I think that I need more help on this. I understand what you are aiming at but I am new to OOP. Could you give me one example? I tried this but it didn't work:

Public Class CHATTERS
Private chatter(10) As chatter
Public Sub AddChatter(ByVal objChatter As chatter, ByVal index As Integer)
chatter(index) = objChatter
End Sub
End Class
Public Class CHATTER
Private _id As String
Private _nick As String
Public Property id() As String
Get
Return _id
End Get
Set(ByVal id As String)
_id = id
End Set
End Property
Public Property nick() As String
Get
Return _nick
End Get
Set(ByVal nick As String)
_nick = nick
End Set
End Property
End Class

I tried this in the serialize function:

Dim chatters As New CHATTERS
For i As Integer = 0 To 9
Dim chatter As New CHATTER
chatter.id = &quot;1&quot;
chatter.nick = &quot;John&quot;
chatters.AddChatter(chatter, i)
Next i

xs_ONLINE.Serialize(sw, chatters)
fMain.TextBox1.Text = sw.ToString

Any more help would be appreciated! thanks / Henrik
 
By the way. I posted this at the wrong place. I am using Visual Basic.NET.
 
First of all:
Have the CHATTERS class itself decide where to put the new CHATTER object. Use a Collection object or a Dictionary object for that, or simply use an array which you can dynamically extend.

Second:
I don't know how the Serialize function has been imlpemented. But I think you should make a property in the CHATTERS collection class from which you can retrieve a CHATTER object, by specifying an index. You can then loop through the CHATTER objects that are contained in the CHATTERS class, serializing them all. Or, maybe even better; you can have the CHATTERS class serialize each object it contains by itself.

Greetings,
Rick
 
Sorry, crossposted the last solution.....

You'll probably have to forget about using the Collection- or Dictionary objects then, since .NET probably has its own implementation of something like that.

But the principle of using the collection class containing objects (i.e. CHATTERS containg CHATTER objects) remains the same.

Greetings,
Rick
 
Thank you, I will try that!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top