Solved

Deserialize XML string to object containing array of elements

Posted on 2012-12-29
3
521 Views
Last Modified: 2012-12-31
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.
0
Comment
Question by:cnxmax
  • 2
3 Comments
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 38729447
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
 

Author Comment

by:cnxmax
ID: 38733345
did what you said
worked like a charmthanks
0
 

Author Closing Comment

by:cnxmax
ID: 38733347
good job
thanks
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Visual xHarbour 1 76
changeXy challenge 13 57
listing all functions in JavaScript 19 101
Device same like our heart 12 45
A short article about a problem I had getting the GPS LocationListener working.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now