[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Deserialize XML string to object containing array of elements

Posted on 2012-12-29
3
Medium Priority
?
533 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 2000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
When you discover the power of the R programming language, you are going to wonder how you ever lived without it! Learn why the language merits a place in your programming arsenal.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

868 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