Deserialize XML string to object containing array of elements

I am using vs 2012, vb.net, .net 4.0 and I need to deserialize the following XML:

<?xml version="1.0" encoding="utf-8"?>
<data>
    <head>
        <dts>2012-12-29 16:00:13</dts>
    </head>
    <status>1</status>
    <response>
        <bots>
            <item>
                <id>8</id>
                <url>https://xyz.com/autopalmanager/bots/xyz.asp</url>
                <priority>2</priority>
                <reporting>verbose</reporting>
                <instances>1</instances>
            </item>
            <item>
                <id>12</id>
                <url>https://xyz.com/autopalmanager/bots/xyz.asp</url>
                <priority>2</priority>
                <reporting>basic</reporting>
                <instances>1</instances>
            </item>
            <item>
                <id>28</id>
                <url>https://xyz.com/client/capitaltables/bots/xyz.htp</url>
                <priority>2</priority>
                <reporting>basic</reporting>
                <instances>1</instances>
            </item>
        </bots>
    </response>
    <errors/>
</data>

I created the following structure (in Class Pong):


    <Serializable> Public Structure data
        Public head As Head
        Public status As String
        Public response As Response
    End Structure
    <Serializable> Public Structure Head
        Public dts As String
    End Structure
    <Serializable> Public Structure Response
        Public bots As Bots
    End Structure
    <Serializable> Public Structure Bots
        Public item As Item()
    End Structure
    <Serializable> Public Structure Item
        Public id As String
        Public url As String
        Public priority As String
        Public reporting As String
        Public instances As String
    End Structure

I use the following to deserialize the XML into the Pong.data structure:

    Dim oMasterData As New Pong.data
    ReDim oMasterData.response.bots.item(100)
    Dim serializer = New XmlSerializer(GetType(Pong.data))
    Using reader = New StringReader(sXML)
        oMasterData = DirectCast(serializer.Deserialize(reader), Pong.data)
    End Using

I get the correct information in oMasterData for head.dts and for status but
oMasterData.response.bots.item comes back with a length=0.

I found many examples of similar deserializations but each falls short when
the main structure has several elements and one element is an "array of a structure".

Any code examples (either vb or C#, C++) would be appreciated.
cnxmaxAsked:
Who is Participating?
 
käµfm³d 👽Commented:
These two, as written:

<Serializable> Public Structure Response
    Public bots As Bots
End Structure

<Serializable> Public Structure Bots
    Public item As Item()
End Structure

Open in new window


...aren't going to help you. Basically, in your XML bots is the container for an array of items. Therefore in your code it needs to be as well. Remove the Bots structure altogether and change Response to:

<Serializable>
Public Structure Response
    <XmlArray>
    <XmlArrayItem("item")>
    Public bots As List(Of Item)
End Structure

Open in new window


...and it should deserialize fine.

Imports System.Xml.Serialization
Imports System.IO

Module Module1

    Sub Main()
        Dim sXML As String = File.ReadAllText("input.xml")
        Dim oMasterData As New Pong.data
        Dim serializer = New XmlSerializer(GetType(Pong.data))
        Using reader = New StringReader(sXML)
            oMasterData = DirectCast(serializer.Deserialize(reader), Pong.data)
        End Using
    End Sub

End Module

Public Class Pong
    <Serializable>
    Public Structure data
        Public head As Head
        Public status As String
        Public response As Response
    End Structure

    <Serializable>
    Public Structure Head
        Public dts As String
    End Structure

    <Serializable>
    Public Structure Response
        <XmlArray>
        <XmlArrayItem("item")>
        Public bots As List(Of Item)
    End Structure

    <Serializable>
    Public Structure Item
        Public id As String
        Public url As String
        Public priority As String
        Public reporting As String
        Public instances As String
    End Structure
End Class

Open in new window


Note:  The <Serializable> attribute is only needed if you are performing binary serailization. It has no bearing on XML serialization. You can safely remove it, but it doesn't hurt anything if you leave it in.
0
 
cnxmaxAuthor Commented:
did what you said
worked like a charmthanks
0
 
cnxmaxAuthor Commented:
good job
thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.