Solved

How can i import this XML file?

Posted on 2014-03-09
6
490 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
Industry Leaders: 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

688 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