XML Parse Elements中最后一次出现的另一个元素

时间:2013-03-22 11:30:16

标签: .net xml vb.net

我对XML很新,并且无法使用VB解析下面的文件。我想要做的是,转到文件中的最后一个<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>

我到目前为止:

'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)

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

这样怎么样?

'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

答案 1 :(得分:0)

解决了它,感谢Zaf指出我正确的方向。而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