Solved

How can i import this XML file?

Posted on 2014-03-09
6
485 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 45

Accepted Solution

by:
aikimark earned 500 total points
ID: 39916450
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
ID: 39916794
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
ID: 39917362
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:nijmedw
ID: 39917535
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
ID: 39917612
you need to change the raceID values as I mentioned earlier.
0
 

Author Closing Comment

by:nijmedw
ID: 39938054
This worked out perfectly.

Thanks a lot.

Edward
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

726 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