如何遍历一个ExpandoObject并获取具体的值
How to traverse an ExpandoObject and get specific values
我有以下XML
(部分)
<?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>
我正在使用 Dandraka XML-Utilities 使 XML 成为 ExpandoObject。这应该让我很容易获得特定的价值。例如,使用上面的方法我可以得到这样的定义日期和版本:
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)
但是,我很难深入了解。
注释掉的都试过了
'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
这就是 def 变量的样子
任何指点将不胜感激。具体迭代更改。
由@TnTinMn 解决。使用了错误的外壳。这是它的工作原理
For Each change As Object In def.changes.changeList
Console.WriteLine(change.number)
Next
我有以下XML (部分)
<?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>
我正在使用 Dandraka XML-Utilities 使 XML 成为 ExpandoObject。这应该让我很容易获得特定的价值。例如,使用上面的方法我可以得到这样的定义日期和版本:
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)
但是,我很难深入了解。 注释掉的都试过了
'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
这就是 def 变量的样子
任何指点将不胜感激。具体迭代更改。
由@TnTinMn 解决。使用了错误的外壳。这是它的工作原理
For Each change As Object In def.changes.changeList
Console.WriteLine(change.number)
Next