Hi guys,
I'm having trouble with serialization especially of array elements!
What I have is an array of clsUsers which have various properties as
shown in the code below
The XML I want to achieve is shown at the top and underneath that is
the xml i have achieved.... any help you can provide would be most
helpful!
Dave.
Code to Follow:
----------------------------------------------------------------
'''------Need to Achieve-------
'''<user_profil es>
''' <user>
''' <uid val="1" />
''' <username val="Dave" />
''' <password val="test" />
''' <level val="1" />
''' </user>
''' <user>
''' .
''' .
''' </user>
''' etc.
'''</user_profiles>
'''---------Actually Achieve--------
'''<ArrayOfClsU ser>
''' <clsUser>
''' <username>1</username>
''' <uid>1</uid>
''' <password>1</password>
''' <level>OPERATOR </level>
''' </clsUser>
''' <clsUser>
''' <username>2</username>
''' <uid>2</uid>
''' <password>2</password>
''' <level>OPERATOR </level>
''' </clsUser>
''' etc.
'''</ArrayOfClsUser>
Imports System.Xml
Imports System.Xml.Seri alization
'<XmlRoot(Eleme ntName:="user_p rofiles", DataType:="anyU RI",
IsNullable:=Fal se, Namespace:="")> _
Public Class clsUserTable
<XmlElement(Ele mentName:="user _profiles")> _
Public allUsers As clsUser()
Private serializer As XmlSerializer
Private Sub SerializeUsers( )
serializer = New XmlSerializer(a llUsers.GetType ())
Dim ns As New XmlSerializerNa mespaces
ns.Add("", "")
Dim sb As New System.Text.Str ingBuilder
Dim tr As New XmlTextWriterFo rmattedNoDeclar ation(New
IO.StringWriter (sb))
serializer.Seri alize(tr, allUsers, ns)
Console.WriteLi ne(sb.ToString( ))
End Sub
Private Sub DeserializeUser s()
serializer = New XmlSerializer(M e.GetType())
Dim o As Object = serializer.Dese rialize(New
IO.FileStream(A pplication.Star tupPath & "\XMLData\xmlUs ers.xml",
IO.FileMode.Ope n))
Dim t As clsUserTable = CType(o, clsUserTable)
allUsers = t.allUsers
End Sub
Private Class XmlTextWriterFo rmattedNoDeclar ation
Inherits System.Xml.XmlT extWriter
Public Sub New(ByVal w As System.IO.TextW riter)
MyBase.New(w)
Formatting = System.Xml.Form atting.Indented
End Sub
Public Overloads Overrides Sub WriteStartDocum ent()
End Sub
End Class
End Class
'<XmlRoot(Eleme ntName:="user") > _
Public Class clsUser
<XmlElement(Ele mentName:="user name")> _
Public _username As String
<XmlElement(Ele mentName:="uid" )> _
Public _UserIndex As Integer
<XmlElement(Ele mentName:="pass word")> _
Public _password As String
<XmlIgnore()> _
Public _Status As enumStatusLevel
<XmlElement(Ele mentName:="leve l")> _
Public _Oplevel As enumOperatorLev el
<XmlIgnore()> _
Public Property UserName() As String
Get
Return _username
End Get
Set(ByVal Value As String)
_username = Value
End Set
End Property
<XmlIgnore()> _
Public Property UserIndex() As Integer
Get
Return _UserIndex
End Get
Set(ByVal Value As Integer)
_UserIndex = Value
End Set
End Property
<XmlIgnore()> _
Public Property Password() As String
Get
Return _password
End Get
Set(ByVal Value As String)
_password = Value
End Set
End Property
<XmlIgnore()> _
Public Property OpLevel() As enumOperatorLev el
Get
Return _Oplevel
End Get
Set(ByVal Value As enumOperatorLev el)
_Oplevel = Value
End Set
End Property
<XmlIgnore()> _
Public Property Status() As enumStatusLevel
Get
Return _Status
End Get
Set(ByVal Value As enumStatusLevel )
_Status = Value
End Set
End Property
Public Overrides Function ToString() As String
Return UserName
End Function
Public Enum enumOperatorLev el
SUPERVISOR = 1
OPERATOR = 0
End Enum
Public Enum enumStatusLevel
ACTIVE = 0
INACTIVE = 1
End Enum
End Class