[使用VB.net根据一个特定值进行XML读取

问题描述 投票:0回答:1

我下面有示例XML。

我需要迭代“ SAMPLE”标签并获取“ NAME”和“ PACKAGE”详细信息。我将详细信息存储在变量中。接下来,我需要检查“ FRAME”的详细信息,我应该在“ FRAME”下获取与“ NAME”匹配的“ NAME”值。在“示例”下。如果匹配,则需要“ IDENTITY”值。我有尝试的示例代码,请帮助我完成流程。

上下文

     <SET>
       <SAMPLE>
         <NAME>sample01</NAME>
         <PACKAGE>01</PACKAGE>
       </SAMPLE>
       <SAMPLE>
         <NAME>sample01</NAME>
         <PACKAGE>01</PACKAGE>
       </SAMPLE>
       <FRAME>
         <NAME>sample01</NAME>
        <IDENTITY>01</IDENTITY>
       </FRAME>
       <FRAME>
         <NAME>sample01</NAME>
        <IDENTITY>01</IDENTITY>
       </FRAME>
     </SET>

我尝试过的代码。

     Dim oXMLDOC As Object = 
                  Nothing
     oXMLDOC =  
     CreateObject
        ("Microsoft.XMLDOM")
     bOK=oXMLDOC. 
         Load("sample.arxml")
   Dim nodes1,nodes2
   Dim node1,node2 

   nodes1 = 
   oXMLDOC.SelectNodes("//SET")
  For each node1 in nodes1
     nodes2 = 
  node1.SelectNodes("//SAMPLE")
     If node2.tagName = "NAME" 
                          Then
       str_name = node2.text
       nodes3=  node1
        .SelectNodes("//FRAME")
       For each node3 in nodes3
        If node3.tagName = 
                         "NAME"
          str_Frame_Name=  
                     node3.text
          IF str_Frame_Name = 
                  str_name Then
                  'I need the "IDENTITY" detail
          End If
        End If
       Next

    Elsif node2.tagName = 
                 "PACKAGE" Then 
      str_package = node2.text
    End If
 Next
xml vb.net xml-parsing
1个回答
0
投票

使用xml linq:

Imports System.Xml
Imports System.Xml.Linq
Module Module1
    Const FILENAME As String = "c:\temp\test.xml"
    Sub Main()
        Dim doc As XDocument = XDocument.Load(FILENAME)

        Dim results = doc.Descendants("SAMPLE").Select(Function(x) New With { _
                                                           .name = CType(x.Element("NAME"), String),
                                                           .package = CType(x.Element("PACKAGE"), String)
                                                       }).ToList()
    End Sub
End Module
© www.soinside.com 2019 - 2024. All rights reserved.