【问题标题】:How to control and get specific data when read XML file读取 XML 文件时如何控制和获取特定数据
【发布时间】:2020-12-10 17:19:07
【问题描述】:

下午好, 我有这个 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和他的值

【问题讨论】:

    标签: vb.net vb.net-2010 xmlreader


    【解决方案1】:

    使用多种功能的示例。它根据项目选择节点。然后它会获取该项目的状态。

        ' 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
    

    【讨论】:

    • 谢谢,您展示了 linq-xml-overview 的 C# 教程。 XML中有吗?或者我在哪里可以看到更多使用 linq 的例子?因为如果我想在 itemstate 上穿上组中的每个项目,我需要做什么?
    • @PedroPinheiro - 在页面顶部应该有一个下拉菜单可以让您更改语言。我的说VB。点击链接了解更多信息。你也可以利用这个搜索docs.microsoft.com/en-us/search/?terms=xelement&scope=.NET
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-26
    • 2014-05-08
    相关资源
    最近更新 更多