?
Solved

Deserialize XML string to object containing array of elements

Posted on 2012-12-29
3
Medium Priority
?
529 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
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…
Six Sigma Control Plans
Suggested Courses

752 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