Solved

How can i import this XML file?

Posted on 2014-03-09
6
471 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
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
XML XSL Choose example 3 19
How would you add MULTITHREADING to the attached C# code? 4 51
VS.net 2010 11 23
Reset asp.net login password 4 29
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

911 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

22 Experts available now in Live!

Get 1:1 Help Now