Reading from an XML file using names

Bozzy

Active member
Joined
Aug 31, 2007
Messages
27
Programming Experience
Beginner
Hi,

I have the following XML file:
VB.NET:
  <!--Action-->
  <Genre Name ="Action" Description ="Action games are perhaps the most basic of gaming genres, and certainly one of the broadest. Action games are characterized by gameplay with emphasis on combat." Picture ="">
    
    <!--Action Adventure-->
    <SubGenre Name="Action-Adventure" Description ="Action-adventure games focus on exploration and usually involve item gathering, simple puzzle solving, and combat." Picture ="">
      <SubGenre Name ="Stealth" Description="Stealth games are a somewhat more recent genre, sometimes referred to as 'sneakers' or 'creepers' to make contrast with the action-oriented 'shooter' sub-genre."/>
      <SubGenre Name ="Survival Horror" Description ="Survival horror games focus on fear and attempt to scare the player via traditional horror fiction elements such as atmospherics, death, the undead, blood and gore."/>
      
      <Example Name="The Legend of Zelda" URL="http://en.wikipedia.org/wiki/The_Legend_of_Zelda_series"/>
      <Example Name="The Metroid Series" URL="http://en.wikipedia.org/wiki/Metroid_%28series%29"/>
    </SubGenre>

    <!--Beat 'em Up-->
    <SubGenre Name="Beat 'em up" Description ="Side-scrolling beat 'em up (or hack and slash) games have an emphasis on one-on-many close quarters combat, beating large numbers of computer-controlled enemies. Gameplay involves the player fighting through a series of increasingly difficult levels." Picture ="">
      <Example Name="The Dynasty Warriors Series" URL="http://en.wikipedia.org/wiki/Dynasty_Warriors"/>
      <Example Name="Final Fight" URL="http://en.wikipedia.org/wiki/Final_Fight"/>
    </SubGenre>

    <!--Fighting-->
    <SubGenre Name="Fighting" Description ="Competitive fighting games (also known as beat 'em ups, particularly in the UK) emphasize one-on-one combat between two characters, one of whom may be computer controlled." Picture ="">
      <Example Name="Super Smash Bros Series" URL="http://en.wikipedia.org/wiki/Super_Smash_Bros._%28series%29"/>
      <Example Name="Tekken Series" URL="http://en.wikipedia.org/wiki/Tekken_%28series%29"/>
    </SubGenre>

    <!--Maze-->
    <SubGenre Name="Maze" Description ="Maze games have a playing field which is entirely a maze, which players must navigate" Picture ="">
    <Example Name="PacMan" URL="http://en.wikipedia.org/wiki/Pac-Man"/>
    </SubGenre>

    <!--Platform-->
    <SubGenre Name="Platform" Description ="Platform games, also called platformers, are games in which the gameplay involves traveling between platforms by jumping (very occasionally other means are substituted for jumping, like swinging or bouncing, but these are considered variations on the same mechanic)."Picture ="">
      <Example Name="The Mario Series" URL="http://en.wikipedia.org/wiki/Mario_%28series%29"/>
      <Example Name="The 'Sonic the Hedgehog' Series" URL="http://en.wikipedia.org/wiki/Sonic_the_Hedgehog_%28series%29"/>
    </SubGenre>
    
    <!--Shooter-->
    <SubGenre Name="Shooter" Description ="It includes many subgenres that have the commonality of focusing on the actions of the avatar using some sort of weapon." Picture ="">
      <SubGenre Name="First-person shooter" Description="First-person shooter video games, commonly known as FPSs, emphasize shooting and combat from the perspective of the character controlled by the player. This perspective is meant to give the player the feeling of 'being there', and allows the player to focus on aiming. Most FPSs are very fast-paced and require quick reflexes on high difficulty levels."/>
      <SubGenre Name="MMOFPS" Description="Massively multiplayer online first person shooter games (MMOFPS) are a genre of Massively multiplayer online games that combines first-person shooter gameplay with a virtual world in which a large number of players may interact over the Internet "/>
      <SubGenre Name="Third-person shooter" Description="Third-person shooter video games, known as TPSs or 3PSs, emphasize shooting and combat from a third-person perspective of the character the player is controlling."/>
      <SubGenre Name="Tactical shooter" Description="A tactical shooter is a subgenre of video game derived from the first-person shooter (FPS) or third-person shooter genres that generally simulate more realistic, squad-based or man-to-man skirmishes."/>
      <SubGenre Name="Shoot 'em up" Description="Shoot 'em ups' or 'shmups' place emphasis on shooting, and usually simplify other gameplay aspects of in deference to this."/>
      
       
      <Example Name="" URL=""/>
      <Example Name="" URL=""/>
    </SubGenre>
    
  </Genre>


And I want to read data from that XML file (there are other genres too, but I have not included them as they are more of the same)

I want to put all the "Genre" items into a combobox, and I want to put the sub-genres into a listview with the picture.


How would I read all this XML etc?


Cheers,
Bozzy
 
This is the code I use to load project files for the game designer program Bozzy:

VB.NET:
lblStatus.Text = "Loadng Project..."
        Dim increment As Integer
        dlgXML.ShowDialog()
        Dim doc As New Xml.XmlDocument()
        Try
            doc.Load(dlgXML.FileName)
        Catch ex As Xml.XmlException
            MessageBox.Show("The selected project file is either invalid or corrupt. Ensure it was created using Theta Game Designer and hasn't been manually edited")
            Return
        End Try
        projectname = dlgXML.SafeFileName.Remove(dlgXML.SafeFileName.Length - 4, 4)
        Dim element As Xml.XmlElement

        Me.Text += " - " & dlgXML.SafeFileName.Remove(dlgXML.SafeFileName.Length - 4, 4)
        ProjectLoader.Show()
        increment = 100 / doc.GetElementsByTagName("Document").Count

        For Each element In doc.GetElementsByTagName("Document")
            Dim document As New Document
            'Increase Progress Bar
            ProjectLoader.ProgressBar1.Value += increment
            'UPdate % Label
            ProjectLoader.Label4.Text = ProjectLoader.ProgressBar1.Value & "%"
            'Load Document Name
            document.Name = element.Attributes.GetNamedItem("Name").Value
            'Current Document
            ProjectLoader.Label2.Text = element.Attributes.GetNamedItem("Name").Value
            ToolStripStatusLabel3.Text = element.Attributes.GetNamedItem("Name").Value
            'Load Document Body
            document.Body = element.Attributes.GetNamedItem("Body").Value
            'Load Document Description
            document.Description = element.Attributes.GetNamedItem("Description").Value
            'Load Document Summary
            document.Summary = element.Attributes.GetNamedItem("Summary").Value
            'Add Document To Document List
            doclist.Add(document)
            'Check if finished
            If ProjectLoader.ProgressBar1.Value >= increment * doc.GetElementsByTagName("Document").Count Then
                ProjectLoader.Close()
                lblStatus.Text = "Ready"
                ToolStripStatusLabel3.Text = "None"
                'Enable Treeview
                treePages.Enabled = True
                Return
            End If
        Next

Just pay attention to the XML parts, the rest are used for a dialog box that opens up with a progress bar and label showing the current document content being loaded and the percentage loaded so far.
 
I would dataSet.ReadXml("the file path") and then look at it in the DataSet Visualizer to see where the data I wanted was

[Bozzy:] Note that your XML isnt valid..
..and excessive use of attributes rather than elements makes it look a little confusing
 
Last edited by a moderator:
Back
Top