Hi,
I am now trying deserialize my employee objects in my XML document into an instance of a class.
Here is the class I wish to create an instance of:
Here is the XML document I am using:
Finally here is my function that I am trying to get to work:
When it runs it get as far as employeeAdder = CType(serializer.Deserialize(xmlReader), EmployeeAdder) and then throws this exception:
Does anyone know what I am doing wrong here?
Thanks, Dave.
I am now trying deserialize my employee objects in my XML document into an instance of a class.
Here is the class I wish to create an instance of:
Code:
Public Class EmployeeAdder
#Region "Declarations"
Private _ID As Int32 = 0
Private _Name As String = String.Empty
Private _ActivityID As Int32 = 0
#End Region
#Region "Properties"
Public Property ID() As Int32
Get
Return _ID
End Get
Set(ByVal value As Int32)
_ID = value
End Set
End Property
Public Property Name() As String
Get
Return _Name
End Get
Set(ByVal value As String)
_Name = value
End Set
End Property
Public Property ActivityID() As Int32
Get
Return _ActivityID
End Get
Set(ByVal value As Int32)
_ActivityID = value
End Set
End Property
#End Region
End Class
Here is the XML document I am using:
Code:
<Employees xmlns="http://www.me.com">
<Employee>
<ID>11</ID>
<Name>Dave Jones</Name>
<ActivityID>9</ActivityID>
</Employee>
<Employee>
<ID>111</ID>
<Name>Wayne Wallice</Name>
<ActivityID>1</ActivityID>
</Employee>
<Employee>
<ID>1111</ID>
<Name>Justin Davies</Name>
<ActivityID>2</ActivityID>
</Employee>
<Employee>
<ID>11111</ID>
<Name>Matthew Jones</Name>
<ActivityID>0</ActivityID>
</Employee>
<Employee>
<ID>111111</ID>
<Name>Steve Pear</Name>
<ActivityID>2</ActivityID>
</Employee>
</Employees>
Finally here is my function that I am trying to get to work:
Code:
Private Function AddEmployee(ByVal xmlDoc As XmlDocument) As Boolean
Get root node and process its children.
Try
Dim xmlNodeEmployees As XmlNode = xmlDoc.FirstChild
For Each node As XmlNode In xmlNodeEmployees.ChildNodes
Dim buffer() As Byte = System.Text.ASCIIEncoding.ASCII.GetBytes(node.InnerXml)
Dim memstrm As New System.IO.MemoryStream(buffer)
Dim xmlReader As New XmlTextReader(memstrm)
Dim employeeAdder As New EmployeeAdder
Dim serializer As New XmlSerializer(GetType(EmployeeAdder))
employeeAdder = CType(serializer.Deserialize(xmlReader), EmployeeAdder)
Next
Catch ex As Exception
Return False
End Try
End Function
When it runs it get as far as employeeAdder = CType(serializer.Deserialize(xmlReader), EmployeeAdder) and then throws this exception:
InnerException = {"<ID xmlns=http://www.me.com> was not expected."}
Does anyone know what I am doing wrong here?
Thanks, Dave.