cnxmax
asked on
Deserialize XML string that contains an array of "an 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>
-<apps>
-<item>
<name>xxx</name>
<folder>c:\web\xxx</folder >
<version>2.xxx</version>
-<bindings>
<item>www.example.com</item>
<item>beta.xxx.com</item>
</bindings>
</item>
-<item>
<name>xxx</name>
<folder>c:\web\xxx</folder >
<version>3.xxx</version>
-<bindings>
<item>www.example.com</item>
<item>xxx:*</item>
</bindings>
</item>
-<item>
<name>xxx</name>
<folder>c:\web\xxx</folder >
<version>3.xxx</version>
-<bindings>
<item>*</item>
</bindings>
</item>
</apps>
</data>
I created the following structure (in Class Pong):
Public Structure data
<XmlArray>
<XmlArrayItem("item")>
Public apps As List(Of dataItem)
End Structure
Public Structure dataItem
Public name As String
Public folder As String
Public version As String
<XmlArray>
<XmlArrayItem("item")>
Public bindings As List(Of bindingItem)
End Structure
Public Structure bindingItem
Public item As String
End Structure
I use the following to deserialize the XML into the Pong.data structure:
Dim serializer As New XmlSerializer(GetType(Dist ribute.dat a))
Dim settings As New XmlReaderSettings()
Using textReader As New StringReader(sXML)
Using xmlReader__1 As XmlReader = XmlReader.Create(textReade r, settings)
oMasterData = DirectCast(serializer.Dese rialize(xm lReader__1 ), Distribute.data)
End Using
End Using
I get the correct array of information in oMasterData for the various "item" array (name, folder and version) but
the inner array for bindings comes back with a length=0.
I found many examples of similar deserializations but each falls short when
the main structure is an array and in the elements of that array another array occurs.
I had a suspision that a problems occurs when both arrays are called "item".
Any code examples (either vb or C#, C++) would be appreciated.
<?xml version="1.0" encoding="UTF-8"?>
-<data>
-<apps>
-<item>
<name>xxx</name>
<folder>c:\web\xxx</folder
<version>2.xxx</version>
-<bindings>
<item>www.example.com</item>
<item>beta.xxx.com</item>
</bindings>
</item>
-<item>
<name>xxx</name>
<folder>c:\web\xxx</folder
<version>3.xxx</version>
-<bindings>
<item>www.example.com</item>
<item>xxx:*</item>
</bindings>
</item>
-<item>
<name>xxx</name>
<folder>c:\web\xxx</folder
<version>3.xxx</version>
-<bindings>
<item>*</item>
</bindings>
</item>
</apps>
</data>
I created the following structure (in Class Pong):
Public Structure data
<XmlArray>
<XmlArrayItem("item")>
Public apps As List(Of dataItem)
End Structure
Public Structure dataItem
Public name As String
Public folder As String
Public version As String
<XmlArray>
<XmlArrayItem("item")>
Public bindings As List(Of bindingItem)
End Structure
Public Structure bindingItem
Public item As String
End Structure
I use the following to deserialize the XML into the Pong.data structure:
Dim serializer As New XmlSerializer(GetType(Dist
Dim settings As New XmlReaderSettings()
Using textReader As New StringReader(sXML)
Using xmlReader__1 As XmlReader = XmlReader.Create(textReade
oMasterData = DirectCast(serializer.Dese
End Using
End Using
I get the correct array of information in oMasterData for the various "item" array (name, folder and version) but
the inner array for bindings comes back with a length=0.
I found many examples of similar deserializations but each falls short when
the main structure is an array and in the elements of that array another array occurs.
I had a suspision that a problems occurs when both arrays are called "item".
Any code examples (either vb or C#, C++) would be appreciated.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Changed second occurance of item to BindingItem ... all is well.
I have done that in the past using Class definitions with arrays.
The important snippets are:
Open in new window
You see that groups is an array of Group objects. Then the Group class again references another array:
Open in new window
So, we can have one or more Group TAGs inside a subsystem TAG and that can contain one more Field TAGs.
The xml has groups and therein you can have additional (sub) groups and fields. Here is an excerpt of an xml:
Open in new window
Here is the code I sue to serialze/deserialize such xml's:
Open in new window