简体   繁体   English

XML解析元素中最后一次出现的另一个元素

[英]XML Parse Elements in the last occurrence of another element

I am fairly new to XML and having trouble parsing the file below using VB. 我对XML相当陌生,无法使用VB解析下面的文件。 What I want to do is, go to the last <SESSION> tag in the file and loop through the <IMAGE> nodes and get their value. 我要做的是,转到文件中的最后一个<SESSION>标记,并循环遍历<IMAGE>节点并获取它们的值。

<?xml version="1.0" encoding="UTF-8"?>
<MIFORMS_EXPORT>
<SESSION FORM_ID="209688" CID="" ID="HAKKI-LAPTOP_634995474247656192">
    <FIELD NAME="A001DATE_M" />
    <FIELD NAME="A002" />
    <FIELD NAME="A003" />
    <FIELD NAME="A004" />
    <IMAGE NAME="P1" TYPE="PAGE" FORMAT="image/png" RESOLUTION="28.35" DEPTH="24">C:\wamp\www\Kapture.web\ClientBin\metis\images\209688-0-03222013-111042768-HAKKI-LAPTOP_634995474247656192.png</IMAGE>
    <IMAGE NAME="P2" TYPE="PAGE" FORMAT="image/png" RESOLUTION="28.35" DEPTH="24">C:\wamp\www\Kapture.web\ClientBin\metis\images\209688-1-03222013-111042783-HAKKI-LAPTOP_634995474247656192.png</IMAGE>
    <IMAGE NAME="P3" TYPE="PAGE" FORMAT="image/png" RESOLUTION="28.35" DEPTH="24">C:\wamp\www\Kapture.web\ClientBin\metis\images\209688-2-03222013-111042799-HAKKI-LAPTOP_634995474247656192.png</IMAGE>
    <IMAGE NAME="P4" TYPE="PAGE" FORMAT="image/png" RESOLUTION="28.35" DEPTH="24">C:\wamp\www\Kapture.web\ClientBin\metis\images\209688-3-03222013-111042814-HAKKI-LAPTOP_634995474247656192.png</IMAGE>
    <IMAGE NAME="P5" TYPE="PAGE" FORMAT="image/png" RESOLUTION="28.35" DEPTH="24">C:\wamp\www\Kapture.web\ClientBin\metis\images\209688-4-03222013-111042830-HAKKI-LAPTOP_634995474247656192.png</IMAGE>
    <IMAGE NAME="P6" TYPE="PAGE" FORMAT="image/png" RESOLUTION="28.35" DEPTH="24">C:\wamp\www\Kapture.web\ClientBin\metis\images\209688-5-03222013-111042846-HAKKI-LAPTOP_634995474247656192.png</IMAGE>
    <IMAGE NAME="P7" TYPE="PAGE" FORMAT="image/png" RESOLUTION="28.35" DEPTH="24">C:\wamp\www\Kapture.web\ClientBin\metis\images\209688-6-03222013-111042846-HAKKI-LAPTOP_634995474247656192.png</IMAGE>
    <IMAGE NAME="P8" TYPE="PAGE" FORMAT="image/png" RESOLUTION="28.35" DEPTH="24">C:\wamp\www\Kapture.web\ClientBin\metis\images\209688-7-03222013-111042861-HAKKI-LAPTOP_634995474247656192.png</IMAGE>
    <IMAGE NAME="P9" TYPE="PAGE" FORMAT="image/png" RESOLUTION="28.35" DEPTH="24">C:\wamp\www\Kapture.web\ClientBin\metis\images\209688-8-03222013-111042877-HAKKI-LAPTOP_634995474247656192.png</IMAGE>
    <IMAGE NAME="P10" TYPE="PAGE" FORMAT="image/png" RESOLUTION="28.35" DEPTH="24">C:\wamp\www\Kapture.web\ClientBin\metis\images\209688-9-03222013-111042892-HAKKI-LAPTOP_634995474247656192.png</IMAGE>
    <IMAGE NAME="P11" TYPE="PAGE" FORMAT="image/png" RESOLUTION="28.35" DEPTH="24">C:\wamp\www\Kapture.web\ClientBin\metis\images\209688-10-03222013-111042892-HAKKI-LAPTOP_634995474247656192.png</IMAGE>
  </SESSION>
  <SESSION FORM_ID="209688" CID="" ID="HAKKI-LAPTOP_634995479807027960">
    <FIELD NAME="A001DATE_M" />
    <FIELD NAME="A002" />
    <FIELD NAME="A003" />
    <FIELD NAME="A004" />
    <IMAGE NAME="P1" TYPE="PAGE" FORMAT="image/png" RESOLUTION="28.35" DEPTH="24">C:\wamp\www\Kapture.web\ClientBin\metis\images\209688-0-03222013-111944041-HAKKI-LAPTOP_634995479807027960.png</IMAGE>
    <IMAGE NAME="P2" TYPE="PAGE" FORMAT="image/png" RESOLUTION="28.35" DEPTH="24">C:\wamp\www\Kapture.web\ClientBin\metis\images\209688-1-03222013-111944056-HAKKI-LAPTOP_634995479807027960.png</IMAGE>
    <IMAGE NAME="P3" TYPE="PAGE" FORMAT="image/png" RESOLUTION="28.35" DEPTH="24">C:\wamp\www\Kapture.web\ClientBin\metis\images\209688-2-03222013-111944072-HAKKI-LAPTOP_634995479807027960.png</IMAGE>
    <IMAGE NAME="P4" TYPE="PAGE" FORMAT="image/png" RESOLUTION="28.35" DEPTH="24">C:\wamp\www\Kapture.web\ClientBin\metis\images\209688-3-03222013-111944088-HAKKI-LAPTOP_634995479807027960.png</IMAGE>
    <IMAGE NAME="P5" TYPE="PAGE" FORMAT="image/png" RESOLUTION="28.35" DEPTH="24">C:\wamp\www\Kapture.web\ClientBin\metis\images\209688-4-03222013-111944103-HAKKI-LAPTOP_634995479807027960.png</IMAGE>
    <IMAGE NAME="P6" TYPE="PAGE" FORMAT="image/png" RESOLUTION="28.35" DEPTH="24">C:\wamp\www\Kapture.web\ClientBin\metis\images\209688-5-03222013-111944119-HAKKI-LAPTOP_634995479807027960.png</IMAGE>
    <IMAGE NAME="P7" TYPE="PAGE" FORMAT="image/png" RESOLUTION="28.35" DEPTH="24">C:\wamp\www\Kapture.web\ClientBin\metis\images\209688-6-03222013-111944134-HAKKI-LAPTOP_634995479807027960.png</IMAGE>
    <IMAGE NAME="P8" TYPE="PAGE" FORMAT="image/png" RESOLUTION="28.35" DEPTH="24">C:\wamp\www\Kapture.web\ClientBin\metis\images\209688-7-03222013-111944134-HAKKI-LAPTOP_634995479807027960.png</IMAGE>
    <IMAGE NAME="P9" TYPE="PAGE" FORMAT="image/png" RESOLUTION="28.35" DEPTH="24">C:\wamp\www\Kapture.web\ClientBin\metis\images\209688-8-03222013-111944150-HAKKI-LAPTOP_634995479807027960.png</IMAGE>
    <IMAGE NAME="P10" TYPE="PAGE" FORMAT="image/png" RESOLUTION="28.35" DEPTH="24">C:\wamp\www\Kapture.web\ClientBin\metis\images\209688-9-03222013-111944166-HAKKI-LAPTOP_634995479807027960.png</IMAGE>
    <IMAGE NAME="P11" TYPE="PAGE" FORMAT="image/png" RESOLUTION="28.35" DEPTH="24">C:\wamp\www\Kapture.web\ClientBin\metis\images\209688-10-03222013-111944212-HAKKI-LAPTOP_634995479807027960.png</IMAGE>
  </SESSION>
</MIFORMS_EXPORT>

I've got this far: 我已经做到了:

'TASK: Get the pen image PNG name
    'Get the total Session count in XML
    Dim penImageRaw As Int32 = doc.GetElementsByTagName("SESSION").Count            
    For startValue = 0 To penImageRaw
        i+=1
    Next

    'Grab the last session element
    Dim test As XmlNode = doc.GetElementsByTagName("SESSION").Item(i)

Thanks for any help. 谢谢你的帮助。

How about like this? 这样怎么样

'TASK: Get the pen image PNG name

'Get the total Session count in XML
Dim penImageRaw As Int32 = doc.GetElementsByTagName("SESSION").Count

If penImageRaw > 0 Then
  'Grab the last session element (Last one is count-1)
  Dim test As XmlNode = doc.GetElementsByTagName("SESSION").Item(penImageRaw-1)
  '
  'Now find IMAGE nodes in this SESSION node here
  '
Else
  'No session nodes found
End if

Solved it, thanks Zaf for pointing me in the right direction. 解决了这个问题,感谢Zaf向我指出了正确的方向。 And Googlers, here is your final result: Google员工,这是您的最终结果:

    Dim penImageRaw As Int32 = doc.GetElementsByTagName("SESSION").Count

If penImageRaw > 0 Then
  Dim test As XmlNode = doc.GetElementsByTagName("SESSION").Item(penImageRaw-1)
  Dim list As XmlNodeList = test.ChildNodes

For Each node As XmlNode In list
    If String.Equals(node.Name, "IMAGE")

    Dim wtf As String = node.FirstChild.Value

    MsgBox(wtf)
End If
    Next

Else
  MsgBox("No Session nodes found")
End If

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM