读取XML文件时如何控制和获取具体数据
How to control and get specific data when read XML file
下午好,
我有这个 XML 文件:
<SpectraMagicNX_data>
<data_set version="2" cdate="2020-12-10T11:34:52+00:00" observer="10">
<sample>
<group name="Test1">
<item name="article">OA</item>
<item name="number">1</item>
<item name="state:">Fail</item>
</group>
<group name="Test2">
<item name="article">0B</item>
<item name="number">1</item>
<item name="state:">Aprove</item>
</group>
</sample>
</data_set>
</SpectraMagicNX_data>`````
我想当 article 值等于 0B 时,显示 state 值。 同意 在这种情况下
代码
Do While (reader.Read())
Select Case reader.NodeType
Case XmlNodeType.Element
If reader.HasAttributes Then 'Se existirem atributos
While reader.MoveToNextAttribute()
If reader.Value = "OA" Then
MsgBox(reader.Value)
End If
End While
End If
Case XmlNodeType.Text
MsgBox(reader.Value)
End Select
Loop`````
我不知道我是否使用了最好的方法,但我想要一些简单易懂的东西。
Reader.Value 基本上显示每个属性值,我无法控制。所以我想知道如何控制 Group、item 和他的值
使用各种功能的示例。它根据项目选择节点。然后获取该项目的状态。
' https://docs.microsoft.com/en-us/dotnet/standard/linq/linq-xml-overview
Dim xe As XElement
' xe = XElement.Load("path to XML here") 'for production use this
'for testing use literal
xe = <SpectraMagicNX_data>
<data_set version="2" cdate="2020-12-10T11:34:52+00:00" observer="10">
<sample>
<group name="Test1">
<item name="article">OA</item>
<item name="number">1</item>
<item name="state:">Fail</item>
</group>
<group name="Test2">
<item name="article">0B</item>
<item name="number">1</item>
<item name="state:">Aprove</item>
</group>
</sample>
</data_set>
</SpectraMagicNX_data>
' select the first <group> that has an item with a
' name attribute="article" and value of OA
Dim ie As IEnumerable(Of XElement)
' use LINQ
ie = From el In xe...<group>.<item>
Where el.@name = "article" AndAlso el.Value = "OA"
Select el.Parent Take 1
If ie.Count = 1 Then
Dim ItemState As String
ItemState = (From el In ie(0).Elements
Where el.@name = "state:"
Select el.Value).FirstOrDefault.ToString
Stop 'look at ItemState
End If
下午好, 我有这个 XML 文件:
<SpectraMagicNX_data>
<data_set version="2" cdate="2020-12-10T11:34:52+00:00" observer="10">
<sample>
<group name="Test1">
<item name="article">OA</item>
<item name="number">1</item>
<item name="state:">Fail</item>
</group>
<group name="Test2">
<item name="article">0B</item>
<item name="number">1</item>
<item name="state:">Aprove</item>
</group>
</sample>
</data_set>
</SpectraMagicNX_data>`````
我想当 article 值等于 0B 时,显示 state 值。 同意 在这种情况下
代码
Do While (reader.Read())
Select Case reader.NodeType
Case XmlNodeType.Element
If reader.HasAttributes Then 'Se existirem atributos
While reader.MoveToNextAttribute()
If reader.Value = "OA" Then
MsgBox(reader.Value)
End If
End While
End If
Case XmlNodeType.Text
MsgBox(reader.Value)
End Select
Loop`````
我不知道我是否使用了最好的方法,但我想要一些简单易懂的东西。
Reader.Value 基本上显示每个属性值,我无法控制。所以我想知道如何控制 Group、item 和他的值
使用各种功能的示例。它根据项目选择节点。然后获取该项目的状态。
' https://docs.microsoft.com/en-us/dotnet/standard/linq/linq-xml-overview
Dim xe As XElement
' xe = XElement.Load("path to XML here") 'for production use this
'for testing use literal
xe = <SpectraMagicNX_data>
<data_set version="2" cdate="2020-12-10T11:34:52+00:00" observer="10">
<sample>
<group name="Test1">
<item name="article">OA</item>
<item name="number">1</item>
<item name="state:">Fail</item>
</group>
<group name="Test2">
<item name="article">0B</item>
<item name="number">1</item>
<item name="state:">Aprove</item>
</group>
</sample>
</data_set>
</SpectraMagicNX_data>
' select the first <group> that has an item with a
' name attribute="article" and value of OA
Dim ie As IEnumerable(Of XElement)
' use LINQ
ie = From el In xe...<group>.<item>
Where el.@name = "article" AndAlso el.Value = "OA"
Select el.Parent Take 1
If ie.Count = 1 Then
Dim ItemState As String
ItemState = (From el In ie(0).Elements
Where el.@name = "state:"
Select el.Value).FirstOrDefault.ToString
Stop 'look at ItemState
End If