Question Reading/Parsing XML

Zeeprah

New member
Joined
Nov 4, 2012
Messages
2
Programming Experience
Beginner
Sorry for the long XML file, but it's what I'm working with :smile: - it actually contains more races than that, but I've shortened it down to just a few races. What is basically is, is a horse meeting, which contains it's races and the horses which ran in each race.

I'm used to working with and seperating .csv files and have secured that, but the way things are going it's looking like I'm going to have to begin reading XML files and updating the database accordingly. Anyway, I'm a beginner to XML and vb .NET as a whole (a few month experience) and should probably not be jumping into such a complicated XML file so early on - I'll post my code below, and if somebody could point me in the right direction as to where I'm going wrong (probably a lot) and possibly provide me with some code/tutorials that will help me tackle this XML file then it would be much appreciated.

What I'm looking to do is populate three different structures; Meeting, Race and Horse, with the corresponding information from the XML file. I can solve the database updating afterwards by myself, I believe (hopefully) :wink:



Public Sub ExtractionService(ByVal spfile As FileInfo)
        Dim strreader As New StreamReader(_sendpathname & spfile.Name, System.Text.Encoding.UTF8)
        Dim xmlreader As XmlTextReader = New XmlTextReader(strreader)
        xmlreader.WhitespaceHandling = WhitespaceHandling.None

        Dim xmldoc As New XmlDocument()
        xmldoc.Load(xmlreader)

        Dim xml_nodelist As XmlNodeList = xmldoc.GetElementsByTagName("/HorseRacingCard/Meeting/Race/Horse")

        Dim mStatus As String
        Dim mID As Integer
        Dim mCountry As String
        Dim mCourse As String
        Dim mDate As Integer
        Dim mDrawAdvantage As String
        Dim mAdvancedGoing As String


        For Each mxmlnode As XmlNode In xmldoc.GetElementsByTagName("Meeting")
            mStatus = mxmlnode.Attributes.ItemOf("status").InnerText
            mID = mxmlnode.Attributes.ItemOf("id").InnerText
            mCountry = mxmlnode.Attributes.ItemOf("country").InnerText
            mCourse = mxmlnode.Attributes.ItemOf("course").InnerText
            mDate = mxmlnode.Attributes.ItemOf("date").InnerText
            mDrawAdvantage = mxmlnode.SelectSingleNode("DrawAdvantage").InnerText
            mAdvancedGoing = mxmlnode.SelectSingleNode("AdvancedGoing").InnerText

            _meeting.meetingStatus = mStatus
            _meeting.meetingID = mID
            _meeting.country = mCountry
            _meeting.course = mCourse
            _meeting.meetingDate = mDate
            _meeting.drawAdvantage = mDrawAdvantage
            _meeting.advancedGoing = mAdvancedGoing
        Next

    End Sub



This code manages to populate my meeting structure succesfully, but I just can't figure out I would go about populating the Race and Horse structures - I've followed a few tutorials and cannot seem to get my head around it. :( I understand I'm probably taking the wrong route here, and that there is potentially so much more I need to add/change to this in order for it to function the way I wish. Once these additions and changes have been highlighted, I believe I can then research these areas, and use the code which you guys can hopefully provide me with, to try tackle this.

Again, all help is appreciated.

PS: I'm not sure why my vb XCODE didn't work, or it may have done, I don't know, but it doesn't look like it has worked. Sorry about that.


Thanks.
 

Attachments

  • horses.xml.txt
    30.5 KB · Views: 32
Last edited:
Hi,

Here is how you can iterate through the different level of nodes within an XML file. I have just used a msgbox here to display elements but as you have mentioned you should be able to take it from here.

VB.NET:
For Each mxmlnode As XmlNode In xmldoc.GetElementsByTagName("Meeting")
  MsgBox(mxmlnode.Attributes.ItemOf("id").InnerText)
  For Each myRace As XmlNode In mxmlnode.SelectNodes("Race")
    MsgBox(myRace.Attributes.ItemOf("maxRunners").InnerText)
    For Each myHorse As XmlNode In myRace.SelectNodes("Horse")
      MsgBox(myHorse.Attributes.ItemOf("name").InnerText)
    Next
  Next
Next
Cheers,

Ian
 
Hi Ian,

Thank you so much for that. This has helped me a lot, It is very much appreciated.

This thread can now be marked as answered :smile:

Thanks
 
Back
Top