XML import to SQL Server 2005

Posted on 2008-10-22
Last Modified: 2012-06-21
I am tyring to create either a stored procedure in SQL Server 2005 or a VB application that I can call through command line that will import an xml file into SQL Server 2005.  I have hundreds of xml files to import and I was also provided with the Schema in an xsd file.  I have been able to generate the database tables based on the schema, but nothing I have tried works for importing the data.  Does anyone have working sample code for the Import?  Thank You.
Question by:epitches

Expert Comment

by:Sander Stad
Comment Utility
Here is an article that can help you. It is a tutorial how to bulk load xml documents into a database.
LVL 11

Accepted Solution

Muhammad Kashif earned 500 total points
Comment Utility
Following code will read From XML file and write to sql server.
    Public Sub Read_XML_File()

        Dim ds As New DataSet

        'Read File to Data Set


        Dim TableCounter As Integer = 0

        Dim dr As DataRow

        Dim dataTableXml As Data.DataTable

        Dim sqlCmd As String = ""

        Dim ColCounter As Integer = 0

        For TableCounter = 0 To ds.Tables.Count - 1

            dataTableXml = ds.Tables(TableCounter)

            For Each dr In dataTableXml.Rows

                sqlCmd = ""

                sqlCmd = "insert into [" & dataTableXml.TableName & "] ("

                For i As Integer = 0 To dataTableXml.Columns.Count - 1

                    sqlCmd = sqlCmd + dataTableXml.Columns(i).ColumnName.ToString() + ","


                sqlCmd = sqlCmd.Substring(0, sqlCmd.Length - 1) + ") values ("

                For x As Integer = 0 To dataTableXml.Columns.Count - 1

                    Dim d As System.DateTime

                    Dim isDate As Boolean

                    If (DateTime.TryParse(dr(x).ToString(), d)) Then    'Checks If the Column is of Type Date

                        isDate = True


                        isDate = False

                    End If

                    If (isDate) Then

                        sqlCmd = sqlCmd + "'" + Format(CDate(d.ToString), "dd/MMM/yyyy") + "',"

                    ElseIf InStr(dr(x).ToString, "'", CompareMethod.Text) <> 0 Then

                        sqlCmd = sqlCmd + "'" + dr(x).ToString().Replace("'", "''") + "',"


                        sqlCmd = sqlCmd + "'" + dr(x) + "',"

                    End If


                sqlCmd = sqlCmd.Substring(0, sqlCmd.Length - 1) + ");"

                'Now Insert Query is prepared

                'Run This query; Record will be inserted



    End Sub

Open in new window


Author Closing Comment

Comment Utility
Thank you!! This worked great.

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

772 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

10 Experts available now in Live!

Get 1:1 Help Now