读取 XML 文件 VB.NET [英] Read XML File VB.NET

查看:43
本文介绍了读取 XML 文件 VB.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 VB.NET 中读取以下 XML 文件

I'm trying to read the following XML file in VB.NET

<?xml version="1.0" encoding="UTF-8"?>
<mame build="0.129 (Jan  8 2009)" debug="no">
    <game name="puckman" sourcefile="pacman.c">
        <description>PuckMan (Japan set 1, Probably Bootleg)</description>
        <year>1980</year>
        <manufacturer>Namco</manufacturer>
        <rom name="namcopac.6e" size="4096" crc="fee263b3" sha1="87117ba5082cd7a615b4ec7c02dd819003fbd669" region="main" offset="0"/>
        <rom name="namcopac.6f" size="4096" crc="39d1fc83" sha1="326dbbf94c6fa2e96613dedb53702f8832b47d59" region="main" offset="1000"/>
        <rom name="namcopac.6h" size="4096" crc="02083b03" sha1="7e1945f6eb51f2e51806d0439f975f7a2889b9b8" region="main" offset="2000"/>
        <rom name="namcopac.6j" size="4096" crc="7a36fe55" sha1="01b4c38108d9dc4e48da4f8d685248e1e6821377" region="main" offset="3000"/>
        <rom name="pacman.5e" size="4096" crc="0c944964" sha1="06ef227747a440831c9a3a613b76693d52a2f0a9" region="gfx1" dispose="yes" offset="0"/>
        <rom name="pacman.5f" size="4096" crc="958fedf9" sha1="4a937ac02216ea8c96477d4a15522070507fb599" region="gfx1" dispose="yes" offset="1000"/>
        <rom name="82s123.7f" size="32" crc="2fc650bd" sha1="8d0268dee78e47c712202b0ec4f1f51109b1f2a5" region="proms" offset="0"/>
        <rom name="82s126.4a" size="256" crc="3eb3a8e4" sha1="19097b5f60d1030f8b82d9f1d3a241f93e5c75d6" region="proms" offset="20"/>
        <rom name="82s126.1m" size="256" crc="a9cc86bf" sha1="bbcec0570aeceb582ff8238a4bc8546a23430081" region="namco" offset="0"/>
        <rom name="82s126.3m" size="256" crc="77245b66" sha1="0c4d0bee858b97632411c440bea6948a74759746" region="namco" offset="100"/>
        <chip type="cpu" tag="main" name="Z80" clock="3072000"/>
        <chip type="audio" tag="namco" name="Namco" clock="96000"/>
        <display type="raster" rotate="90" width="288" height="224" refresh="60.606061" pixclock="6144000" htotal="384" hbend="0" hbstart="288" vtotal="264" vbend="0" vbstart="224" />
        <sound channels="1"/>
        <input players="2" buttons="1" coins="2" service="yes">
            <control type="joy4way"/>
        </input>
        <dipswitch name="Rack Test (Cheat)">
            <dipvalue name="Off" default="yes"/>
            <dipvalue name="On"/>
        </dipswitch>
        <dipswitch name="Service Mode">
            <dipvalue name="Off" default="yes"/>
            <dipvalue name="On"/>
        </dipswitch>
        <dipswitch name="Cabinet">
            <dipvalue name="Upright" default="yes"/>
            <dipvalue name="Cocktail"/>
        </dipswitch>
        <dipswitch name="Coinage">
            <dipvalue name="2 Coins/1 Credit"/>
            <dipvalue name="1 Coin/1 Credit" default="yes"/>
            <dipvalue name="1 Coin/2 Credits"/>
            <dipvalue name="Free Play"/>
        </dipswitch>
        <dipswitch name="Lives">
            <dipvalue name="1"/>
            <dipvalue name="2"/>
            <dipvalue name="3" default="yes"/>
            <dipvalue name="5"/>
        </dipswitch>
        <dipswitch name="Bonus Life">
            <dipvalue name="10000" default="yes"/>
            <dipvalue name="15000"/>
            <dipvalue name="20000"/>
            <dipvalue name="None"/>
        </dipswitch>
        <dipswitch name="Difficulty">
            <dipvalue name="Normal" default="yes"/>
            <dipvalue name="Hard"/>
        </dipswitch>
        <dipswitch name="Ghost Names">
            <dipvalue name="Normal" default="yes"/>
            <dipvalue name="Alternate"/>
        </dipswitch>
        <driver status="good" emulation="good" color="good" sound="good" graphic="good" savestate="supported" palettesize="512"/>
    </game>
    <game name="puckmana" sourcefile="pacman.c" cloneof="puckman" romof="puckman">
        <description>PuckMan (Japan set 2)</description>
        <year>1980</year>
        <manufacturer>Namco</manufacturer>
        <rom name="pacman.6e" size="4096" crc="c1e6ab10" sha1="e87e059c5be45753f7e9f33dff851f16d6751181" region="main" offset="0"/>
        <rom name="pacman.6f" size="4096" crc="1a6fb2d4" sha1="674d3a7f00d8be5e38b1fdc208ebef5a92d38329" region="main" offset="1000"/>
        <rom name="pacman.6h" size="4096" crc="bcdd1beb" sha1="8e47e8c2c4d6117d174cdac150392042d3e0a881" region="main" offset="2000"/>
        <rom name="prg7" size="2048" crc="b6289b26" sha1="d249fa9cdde774d5fee7258147cd25fa3f4dc2b3" region="main" offset="3000"/>
        <rom name="prg8" size="2048" crc="17a88c13" sha1="eb462de79f49b7aa8adb0cc6d31535b10550c0ce" region="main" offset="3800"/>
        <rom name="chg1" size="2048" crc="2066a0b7" sha1="6d4ccc27d6be185589e08aa9f18702b679e49a4a" region="gfx1" dispose="yes" offset="0"/>
        <rom name="chg2" size="2048" crc="3591b89d" sha1="79bb456be6c39c1ccd7d077fbe181523131fb300" region="gfx1" dispose="yes" offset="800"/>
        <rom name="pacman.5f" merge="pacman.5f" size="4096" crc="958fedf9" sha1="4a937ac02216ea8c96477d4a15522070507fb599" region="gfx1" dispose="yes" offset="1000"/>
        <rom name="82s123.7f" merge="82s123.7f" size="32" crc="2fc650bd" sha1="8d0268dee78e47c712202b0ec4f1f51109b1f2a5" region="proms" offset="0"/>
        <rom name="82s126.4a" merge="82s126.4a" size="256" crc="3eb3a8e4" sha1="19097b5f60d1030f8b82d9f1d3a241f93e5c75d6" region="proms" offset="20"/>
        <rom name="82s126.1m" merge="82s126.1m" size="256" crc="a9cc86bf" sha1="bbcec0570aeceb582ff8238a4bc8546a23430081" region="namco" offset="0"/>
        <rom name="82s126.3m" merge="82s126.3m" size="256" crc="77245b66" sha1="0c4d0bee858b97632411c440bea6948a74759746" region="namco" offset="100"/>
        <chip type="cpu" tag="main" name="Z80" clock="3072000"/>
        <chip type="audio" tag="namco" name="Namco" clock="96000"/>
        <display type="raster" rotate="90" width="288" height="224" refresh="60.606061" pixclock="6144000" htotal="384" hbend="0" hbstart="288" vtotal="264" vbend="0" vbstart="224" />
        <sound channels="1"/>
        <input players="2" buttons="1" coins="2" service="yes">
            <control type="joy4way"/>
        </input>
        <dipswitch name="Rack Test (Cheat)">
            <dipvalue name="Off" default="yes"/>
            <dipvalue name="On"/>
        </dipswitch>
        <dipswitch name="Service Mode">
            <dipvalue name="Off" default="yes"/>
            <dipvalue name="On"/>
        </dipswitch>
        <dipswitch name="Cabinet">
            <dipvalue name="Upright" default="yes"/>
            <dipvalue name="Cocktail"/>
        </dipswitch>
        <dipswitch name="Coinage">
            <dipvalue name="2 Coins/1 Credit"/>
            <dipvalue name="1 Coin/1 Credit" default="yes"/>
            <dipvalue name="1 Coin/2 Credits"/>
            <dipvalue name="Free Play"/>
        </dipswitch>
        <dipswitch name="Lives">
            <dipvalue name="1"/>
            <dipvalue name="2"/>
            <dipvalue name="3" default="yes"/>
            <dipvalue name="5"/>
        </dipswitch>
        <dipswitch name="Bonus Life">
            <dipvalue name="10000" default="yes"/>
            <dipvalue name="15000"/>
            <dipvalue name="20000"/>
            <dipvalue name="None"/>
        </dipswitch>
        <dipswitch name="Difficulty">
            <dipvalue name="Normal" default="yes"/>
            <dipvalue name="Hard"/>
        </dipswitch>
        <dipswitch name="Ghost Names">
            <dipvalue name="Normal" default="yes"/>
            <dipvalue name="Alternate"/>
        </dipswitch>
        <driver status="good" emulation="good" color="good" sound="good" graphic="good" savestate="supported" palettesize="512"/>
    </game>
</mame>

这是我使用的代码

Imports System.IO
Imports System.Xml
Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Try
        Dim m_xmld As XmlDocument
        Dim m_nodelist As XmlNodeList
        Dim m_node As XmlNode

        m_xmld = New XmlDocument()
        m_xmld.Load("C:\mame2.xml")
        m_nodelist = m_xmld.SelectNodes("/mame/game")

        For Each m_node In m_nodelist

            Dim GameAttribute = m_node.Attributes.GetNamedItem("name").Value
            Dim name = m_node.ChildNodes.Item(0).InnerText
            Dim description = m_node.ChildNodes.Item(1).InnerText
            Dim manufacturer = m_node.ChildNodes.Item(2).InnerText
            Dim rotate = m_node.ChildNodes.Item(15).Attributes.GetNamedItem("rotate").Value
            Dim controles = m_node.ChildNodes.Item(17).ChildNodes.Item(0).Attributes.GetNamedItem("type").Value

            MessageBox.Show("ROM: " & GameAttribute _
              & "-- Description: " & name & "-- Year: " _
              & description & "-- Manufacturer: " _
              & manufacturer & "-- Rotate: " _
              & rotate & "-- Control Type: " & controles)

        Next
    Catch errorVariable As Exception

        MessageBox.Show(errorVariable.ToString())
    End Try
    Me.Close()
End Sub
End Class

<小时>

我正在尝试读取名称"、描述"、年份"、制造商"、旋转"和输入玩家控制类型".


I'm trying to read "name", "description","year", "manufacturer", "rotate" and "input player control type".

使用此代码,我可以读取第一个游戏,但不能读取 XML 文件的其余部分(在此示例中,我粘贴了 2 个游戏).我可以通过子节点号读取子节点和属性,问题是rom"中的游戏有不同数量的子节点,程序返回错误.

With this code i can read the first game, but not the rest of XML file (in this example I paste 2 games). I can read the Childnodes and atributes by the childnode number, the problem is that the games in "rom" has different number of childnodes and the program returns me an error.

我想有另一种方式来阅读这份文件,但我不知道是哪种方式.

I imagine there's another way to read this document, but I don't know which is it.

感谢您的提前,并为我的英语感到抱歉.

Thanks for advance and sorry for my english.

推荐答案

您需要使用 XPath 按名称访问子节点,而不是按索引.例如:

You need to access the child nodes by name using XPath, instead of by index. For instance:

Dim name As String = m_node.SelectSingleNode("@name").InnerText
Dim description As String = m_node.SelectSingleNode("description").InnerText
Dim manufacturer As String = m_node.SelectSingleNode("manufacturer").InnerText
Dim rotate As String = m_node.SelectSingleNode("display/@rotate").InnerText
Dim controls As String = m_node.SelectSingleNode("input/control/@type").InnerText

这篇关于读取 XML 文件 VB.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆