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
Solved

How can i import this XML file?

Posted on 2014-03-09
6
479 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
How our DevOps Teams Maximize Uptime

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

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…

839 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