Search code examples
vb.netdictionaryexpandoobject

How to traverse an ExpandoObject and get specific values


I have the following XML (part)

<?xml version="1.0" encoding="utf-8" ?>
<definition date="2021-04-30" version="1.01">
    <changes>
        <change number="1" date="2021-04-30" description="Added .." />
        <change number="2" date="2021-04-30" description="Changes in .." />
        <change number="3" date="2021-04-30" description="Fixed .." />
        <change number="4" date="2021-05-11" description="Added " />
    </changes>
    <general>
        <styles>
            <style name="title">
                <font name="Arial" size="12" bold="true"/>
            </style>
            <style name="general">
                <font name="Courier new" size="10" bold="true" />
            </style>
            <style name="header">
                <font name="Courier new" size="10" bold="false" />
            </style>
        </styles>
    </general>

I'm using Dandraka XML-Utilities to make the XML an ExpandoObject. Which should allow me to get easily to specific values. For instance, working with the above I am able to get the Definition date and version like this:

    Dim strXML As String
    strXML = File.ReadAllText("C:\Tools\ReportDefinitions.xml")
    Dim def As Object
    def = XmlSlurper.ParseText(strXML)
    Console.WriteLine(def.date)
    Console.WriteLine(def.version)

However, I'm having a hard time getting any deeper. Tried all the ones that are commented out


'error For Each c In def.changes                    'unable to cast
'error For Each member In CType(def.changes, IDictionary(Of String, Object)) 'Invalid cast
'error For Each c In def.change                     'missing change
'error For Each c In def.changes.change             'missing change
'error For Each c In def.definition.changes.change  'missing definition
'error Console.WriteLine(def.changes.change(0).number)  'missing member
'Console.WriteLine(def.changes.changelist.numner)
For Each member In CType(def.changes, IDictionary(Of String, Object))
  Console.WriteLine(member.Key)
Next

This is what the def variable looks like Local variables

Any pointers would be much appreciated. Specifically iterating through the changes.


Solution

  • Solved by @TnTinMn. Used the wrong casing. This is how it works

        For Each change As Object In def.changes.changeList
          Console.WriteLine(change.number)
        Next