Solved

Deserialize XML string to object containing array of elements

Posted on 2012-12-29
3
523 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 75

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

920 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

12 Experts available now in Live!

Get 1:1 Help Now