tags:

views:

180

answers:

1

I am trying to read this XML document.

An excerpt:

<datafile xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="wiitdb.xsd"> 
    <WiiTDB version="20100217113738" games="2368"/>     
        <game name="Help Wanted: 50 Wacky Jobs (DEMO) (USA) (EN)"> 
            <id>DHKE18</id> 
            <type/> 
            <region>NTSC-U</region> 
            <languages>EN</languages> 
            <locale lang="EN"> 
                <title>Help Wanted: 50 Wacky Jobs (DEMO)</title> 
                <synopsis/> 
            </locale> 
            <developer>HUDSON SOFT CO., LTD.</developer> 
            <publisher>Hudson Entertainment, Inc.</publisher> 
            <date year="2009" month="" day=""/> 
            <genre>party</genre> 
            <rating type="ESRB" value="E10+"> 
                <descriptor>comic mischief</descriptor> 
                <descriptor>mild cartoon violence</descriptor> 
                <descriptor>mild suggestive themes</descriptor> 
            </rating> 
            <wi-fi players="0"/> 
            <input players="2"> 
                <control type="wiimote" required="true"/> 
                <control type="nunchuk" required="true"/> 
            </input> 
            <rom version="" name="Help Wanted: 50 Wacky Jobs (DEMO) (USA) (EN).iso" size="4699979776"/> 
        </game> 

So far I have this:

    Dim doc as XPathDocument
    Dim nav as XPathNavigator
    Dim iter as XPathNodeIterator
    Dim lstNav As XPathNavigator
    Dim iterNews As XPathNodeIterator

    doc = New XPathDocument("wiitdb.xml")
        nav = doc.CreateNavigator
        iter = nav.Select("/WiiTDB/game") 'Your node name goes here
        'Loop through the records in that node
        While iter.MoveNext
            'Get the data we need from the node
            lstNav = iter.Current
            iterNews = lstNav.SelectDescendants(XPathNodeType.Element, False)
            'Loop through the child nodes
            txtOutput.Text = txtOutput.Text & vbNewLine & iterNews.Current.Name & ": " & iterNews.Current.Value
        End While

It just skips the "While iter.MoveNext" part of the code. I tries it with a simple XML file, and it works fine.

+1  A: 

I think your XPath query is off. WiiTDB is a closed node, so you need to look for /datafile/game or //game.

bryanjonker
how did you determine that?...might I ask.
Saif Khan
Thanks, I fixed that, but it just goes on forever and crashes. Is there a way to make this more efficient? Or make it take less time?
MI
Saif - the formatting is a bit misleading. Nodes are usually <a></a>, or they can be self-closing like <a />. The datafile node is not closed, and the WiiTDB is closed. Therefore, the game node is in the datafile node, so you use XPath to do /datafile/game. //game will find all <game> nodes.
bryanjonker
MI - unfortunately, I think XPathDocument is going to be the least memory intensive way without using LINQ to XML (or another in-line parsing). Is the crash happening when you load the first XPathDocument, or later on in the process? You may be able to get away with not doing an inner loop, and just using XPath to query the information directly.
bryanjonker