Solved

How can i import this XML file?

Posted on 2014-03-09
6
468 Views
Last Modified: 2014-03-18
Hello,

I have a problem. I have a XML file, and want to import it, so i can ue the data in my application. I can import the file, but then i loose al my relations between the records. So can anyone help me out here?

I prefere vb.net, but C# will do.

At this moment i do it with this code, it imports the file to a database, but like is said, i've no relations between the records, like raceid, sessionid, athleteid, etc..

    Dim dsPubs As New DataSet()
        Dim oSQLConn As SqlConnection = New SqlConnection
        If deletetables() = True Then

            Dim strStatus As String = String.Empty
            Try
                oSQLConn.ConnectionString = "Server=localhost\sqlexpress;integrated security=SSPI;database=OCZK;"
                oSQLConn.Open()

                ' Read in XML from file
                dsPubs.ReadXml(Me.TextBox1.Text & "\" & Me.TextBox2.Text)
                If dsPubs.Tables.Count > 0 Then
                    For i As Integer = 0 To dsPubs.Tables.Count() - 1
                        Select Case dsPubs.Tables.Item(i).TableName
                            Case "meet"
                                Me.pbmeet.Minimum = 0
                                Me.pbmeet.Maximum = dsPubs.Tables.Item("meet").Rows.Count()
                                Me.pbmeet.Step = 1

                                For Each dr As DataRow In dsPubs.Tables.Item("meet").Rows
                                    Dim sc As New SqlCommand("INSERT INTO meet " & _
                                                              "VALUES (@Date, @name, @meet_Id, @meetID, @city, @countryCode, @comment)", oSQLConn)
                                    sc.Parameters.AddWithValue("@Date", dr.Item(0))
                                    sc.Parameters.AddWithValue("@name", dr.Item(1))
                                    sc.Parameters.AddWithValue("@meet_Id", dr.Item(2))
                                    sc.Parameters.AddWithValue("@meetID", dr.Item(3))
                                    sc.Parameters.AddWithValue("@city", dr.Item(4))
                                    sc.Parameters.AddWithValue("@countryCode", dr.Item(5))
                                    sc.Parameters.AddWithValue("@comment", dr.Item(6))
                                    sc.ExecuteNonQuery()
                                    Me.pbmeet.PerformStep()

                                Next
                            Case "entries"
                                Me.pbentries.Minimum = 0
                                Me.pbentries.Maximum = dsPubs.Tables.Item("entries").Rows.Count
                                Me.pbentries.Step = 1
                                For Each dr As DataRow In dsPubs.Tables.Item("entries").Rows
                                    Dim sc As New SqlCommand("INSERT INTO entries " & _
                                                              "VALUES (@entries_Id, @Race_Id)", oSQLConn)
                                    sc.Parameters.AddWithValue("@entries_Id", dr.Item(0))
                                    sc.Parameters.AddWithValue("@Race_Id", dr.Item(1))
                                    Me.pbentries.PerformStep()

                                    sc.ExecuteNonQuery()
                                Next
                            Case "Race"
                                For Each dr As DataRow In dsPubs.Tables.Item("Race").Rows
                                    Dim sc As New SqlCommand("INSERT INTO Race " & _
                                                              "VALUES (@raceID, @date, @gender, @length, @relayCount, @unit, @swimStyle, @sessionID, @sessionPosition, @name, @code, @file, @Race_Id, @races_Id)", oSQLConn)
                                    sc.Parameters.AddWithValue("@raceID", dr.Item(0))
                                    sc.Parameters.AddWithValue("@date", dr.Item(1))
                                    sc.Parameters.AddWithValue("@gender", dr.Item(2))
                                    sc.Parameters.AddWithValue("@length", dr.Item(3))
                                    sc.Parameters.AddWithValue("@relayCount", dr.Item(4))
                                    sc.Parameters.AddWithValue("@unit", dr.Item(5))
                                    sc.Parameters.AddWithValue("@swimStyle", dr.Item(6))
                                    sc.Parameters.AddWithValue("@sessionID", dr.Item(7))
                                    sc.Parameters.AddWithValue("@sessionPosition", dr.Item(8))
                                    sc.Parameters.AddWithValue("@name", dr.Item(9))
                                    sc.Parameters.AddWithValue("@code", dr.Item(10))
                                    sc.Parameters.AddWithValue("@file", dr.Item(11))
                                    sc.Parameters.AddWithValue("@Race_Id", dr.Item(12))
                                    sc.Parameters.AddWithValue("@races_Id", dr.Item(13))
                                    sc.ExecuteNonQuery()
                                Next
                            Case "races"
                                For Each dr As DataRow In dsPubs.Tables.Item("races").Rows
                                    Dim sc As New SqlCommand("INSERT INTO races " & _
                                                              "VALUES (@races_Id, @Session_Id)", oSQLConn)
                                    sc.Parameters.AddWithValue("@races_Id", dr.Item(0))
                                    sc.Parameters.AddWithValue("@Session_Id", dr.Item(1))
                                    sc.ExecuteNonQuery()
                                Next
                            Case "Session"
                                For Each dr As DataRow In dsPubs.Tables.Item("Session").Rows
                                    Dim sc As New SqlCommand("INSERT INTO Session " & _
                                                              "VALUES (@name, @Session_Id, @sessionID, @meetID, @meetPosition, @date, @comment, @sessions_Id)", oSQLConn)
                                    sc.Parameters.AddWithValue("@name", dr.Item(0))
                                    sc.Parameters.AddWithValue("@Session_Id", dr.Item(1))
                                    sc.Parameters.AddWithValue("@sessionID", dr.Item(2))
                                    sc.Parameters.AddWithValue("@meetID", dr.Item(3))
                                    sc.Parameters.AddWithValue("@meetPosition", dr.Item(4))
                                    sc.Parameters.AddWithValue("@date", dr.Item(5))
                                    sc.Parameters.AddWithValue("@comment", dr.Item(6))
                                    sc.Parameters.AddWithValue("@sessions_Id", dr.Item(7))

                                    sc.ExecuteNonQuery()
                                Next
                            Case "sessions"
                                For Each dr As DataRow In dsPubs.Tables.Item("sessions").Rows
                                    Dim sc As New SqlCommand("INSERT INTO sessions " & _
                                                              "VALUES (@sessions_Id, @meet_Id)", oSQLConn)
                                    sc.Parameters.AddWithValue("@sessions_Id", dr.Item(0))
                                    sc.Parameters.AddWithValue("@meet_Id", dr.Item(1))
                                    sc.ExecuteNonQuery()
                                Next
                            Case "Entry"
                                For Each dr As DataRow In dsPubs.Tables.Item("Entry").Rows
                                    Dim sc As New SqlCommand("INSERT INTO Entry " & _
                                                              "VALUES (@entryID, @raceID, @lane, @relayPosition, @athleteID, @state, @entries_Id)", oSQLConn)
                                    sc.Parameters.AddWithValue("@entryID", dr.Item(0))
                                    sc.Parameters.AddWithValue("@raceID", dr.Item(1))
                                    sc.Parameters.AddWithValue("@lane", dr.Item(2))
                                    sc.Parameters.AddWithValue("@relayPosition", dr.Item(3))
                                    sc.Parameters.AddWithValue("@athleteID", dr.Item(4))
                                    sc.Parameters.AddWithValue("@state", dr.Item(5))
                                    sc.Parameters.AddWithValue("@entries_Id", dr.Item(6))

                                    sc.ExecuteNonQuery()
                                Next
                            Case "pool"
                                For Each dr As DataRow In dsPubs.Tables.Item("pool").Rows
                                    Dim sc As New SqlCommand("INSERT INTO pool " & _
                                                              "VALUES (@length, @minLane, @maxLane, @timeingmode, @falseStartPad, @rTouchPad, @ITouchPad, @meet_Id)", oSQLConn)
                                    sc.Parameters.AddWithValue("@length", dr.Item(0))
                                    sc.Parameters.AddWithValue("@minLane", dr.Item(1))
                                    sc.Parameters.AddWithValue("@maxLane", dr.Item(2))
                                    sc.Parameters.AddWithValue("@timeingmode", dr.Item(3))
                                    sc.Parameters.AddWithValue("@falseStartPad", dr.Item(4))
                                    sc.Parameters.AddWithValue("@rTouchPad", dr.Item(5))
                                    sc.Parameters.AddWithValue("@ITouchPad", dr.Item(6))
                                    sc.Parameters.AddWithValue("@meet_Id", dr.Item(7))

                                    sc.ExecuteNonQuery()
                                Next
                            Case "clubs"
                                For Each dr As DataRow In dsPubs.Tables.Item("clubs").Rows
                                    Dim sc As New SqlCommand("INSERT INTO clubs " & _
                                                              "VALUES (@clubs_Id, @meet_Id)", oSQLConn)
                                    sc.Parameters.AddWithValue("@clubs_Id", dr.Item(0))
                                    sc.Parameters.AddWithValue("@meet_Id", dr.Item(1))
                                    sc.ExecuteNonQuery()
                                Next
                            Case "Club"
                                For Each dr As DataRow In dsPubs.Tables.Item("Club").Rows
                                    Dim sc As New SqlCommand("INSERT INTO Club " & _
                                                              "VALUES (@clubID, @Club_Id, @name, @clubs_Id)", oSQLConn)
                                    sc.Parameters.AddWithValue("@clubID", dr.Item(0))
                                    sc.Parameters.AddWithValue("@Club_Id", dr.Item(1))
                                    sc.Parameters.AddWithValue("@name", dr.Item(0))
                                    sc.Parameters.AddWithValue("@clubs_Id", dr.Item(1))
                                    sc.ExecuteNonQuery()
                                Next
                            Case "athletes"
                                For Each dr As DataRow In dsPubs.Tables.Item("athletes").Rows
                                    Dim sc As New SqlCommand("INSERT INTO athletes " & _
                                                              "VALUES (@athletes_Id, @Club_Id)", oSQLConn)
                                    sc.Parameters.AddWithValue("@athletes_Id", dr.Item(0))
                                    sc.Parameters.AddWithValue("@Club_Id", dr.Item(1))
                                    sc.ExecuteNonQuery()
                                Next
                            Case "Athlete"
                                For Each dr As DataRow In dsPubs.Tables.Item("Athlete").Rows
                                    Dim sc As New SqlCommand("INSERT INTO Athlete " & _
                                                              "VALUES (@Athlete_Id, @athleteID, @gender, @birthdate, @nation, @clubUID, @licence, @athletes_Id)", oSQLConn)
                                    sc.Parameters.AddWithValue("@Athlete_Id", dr.Item(0))
                                    sc.Parameters.AddWithValue("@athleteID", dr.Item(1))
                                    sc.Parameters.AddWithValue("@gender", dr.Item(2))
                                    sc.Parameters.AddWithValue("@birthdate", dr.Item(3))
                                    sc.Parameters.AddWithValue("@nation", dr.Item(4))
                                    sc.Parameters.AddWithValue("@clubUID", dr.Item(5))
                                    sc.Parameters.AddWithValue("@licence", dr.Item(6))
                                    sc.Parameters.AddWithValue("@athletes_Id", dr.Item(7))
                                    sc.ExecuteNonQuery()
                                Next
                            Case "contact"
                                For Each dr As DataRow In dsPubs.Tables.Item("contact").Rows
                                    Dim sc As New SqlCommand("INSERT INTO contact " & _
                                                              "VALUES (@Name, @firstname, @Athlete_Id)", oSQLConn)
                                    sc.Parameters.AddWithValue("@Name", dr.Item(0))
                                    sc.Parameters.AddWithValue("@firstname", dr.Item(1))
                                    sc.Parameters.AddWithValue("@Athlete_Id", dr.Item(2))

                                    sc.ExecuteNonQuery()
                                Next
                        End Select
                    Next
                End If
                MsgBox("Klaar")
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End If

       

Open in new window

I want to have a database with one table with the sessions, one table with the athletes and one table matching all os this in a race, with eacht athlete on a lane, etc..
The target database i made by myself, so if the suggestion would be to change is, this can be done.

Attached, i have my XML file to import.

Thanks a lot.

Edward
meetdemo.xml
0
Comment
Question by:nijmedw
  • 3
  • 3
6 Comments
 
LVL 45

Accepted Solution

by:
aikimark earned 500 total points
Comment Utility
If I understand your problem, you need to relate the rows in the tables.  The first problem I see is in the Race structure.  Here, the raceID values are sequential values and do not help you relate the entry values' raceID values.  I think if you parse each race's
<file>Id_2227-Event_1-Heat_1</file>
element and get the value between "Id_" and "-Event", you could replace the sequential number with this value, "2227" in this case, to relate these two tables.

You would still have the race items' code values if you needed to use it/them.

The sessions and races are adequately linked through the sessionID element.

From your posted XML sample, it seems that you have some missing athlete data in the athletes section.
0
 

Author Comment

by:nijmedw
Comment Utility
This is correct, the file is an example of how the structure looks like. But due to privacy reasons of other people i didn't publish the whole XML, only a few records.
The point is, when you see the XML file, you see the structure, like sessions, with their name, within the session the races, within the races the individual race, than the lane and the athlate that will be in that lane.

So, in my example i just added a few athletes, but the structure is correct.

Thanks,

Edward
0
 
LVL 45

Expert Comment

by:aikimark
Comment Utility
On the surface, it looks like your code should work.  I would have done a different implementation, looping through the fields/columns, rather than hard coding each field's data transfer to the parameter.

I'm not sure, but there might also be a way to 'map' the data into your database.
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:nijmedw
Comment Utility
Thanks for your reply, the code works, but i loose my relation between session/Race/Entries

If i do a simple import straight to a dataset, the system different tables, like Race, races, Session,sessions,Entry, entries, and then i losse my relations.

Thanks,

Edward
0
 
LVL 45

Expert Comment

by:aikimark
Comment Utility
you need to change the raceID values as I mentioned earlier.
0
 

Author Closing Comment

by:nijmedw
Comment Utility
This worked out perfectly.

Thanks a lot.

Edward
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now