Solved

Inserting XML into SQL Server Table

Posted on 2014-04-09
4
253 Views
Last Modified: 2014-04-11
I have a function as below however I now need to write a similar one that inserts the XML into a SQL server 2008 table.  How is this achieved?


  Public Shared Function WriteXMLToDisk(ByVal Despatches As Generic.List(Of XMLDespatch), ByVal FilePath As String) As Boolean
        'XElement xmlCustomers = new XElement(" customers", from c in customers where c.Country = = Countries.Italy select new XElement(" customer", new XElement(" name", c.Name), new XElement(" city", c.City), new XElement(" country", c.Country)));

        Try
            Dim XMLDespatches As XElement = New XElement("Despatches", From d In Despatches _
                                                                             Select New XElement("Despatch", _
                                                                             New XAttribute("LongCode", d.LongCode), _
                                                                             New XAttribute("Memo", d.Memo), _
                                                                             New XAttribute("OtherBranch", d.OtherBranch), _
                                                                             New XAttribute("TransactionDate", d.TransactionDate), _
                                                                             New XAttribute("TransactionType", d.TransactionType), _
                                                                             New XAttribute("Units", d.Units.ToString)))

            If Not Directory.Exists(CommonAttributes.DespatchesPath) Then
                Directory.CreateDirectory(CommonAttributes.DespatchesPath)
            End If

            If File.Exists(FilePath) Then
                File.Delete(FilePath)
            End If

            XMLDespatches.Save(FilePath, SaveOptions.None)
            Return True
        Catch ex As Exception
            Return False
        End Try

    End Function

Open in new window

0
Comment
Question by:Alyanto
  • 2
  • 2
4 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39989272
can you please clarify what exactly the issue is?
to save "xml" into a database, you need the following scripts:
* build the XML (same as above, except that you don't .Save to a file, but I presume the object has a property returning the full xml), so no issue here?
* connect to the database, which has the table with the xml column
* performing the INSERT / UPDATE using the above xml string
0
 

Author Comment

by:Alyanto
ID: 39990264
So what you are saying is that it is no diferent to anyother database transaction.

Declare  a SQLConnection, Declare a SQLSQLCommand etc... and then run an insert query or Stored Proc.  Nothing special, no specific way of couching the parameters etc?
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 39990726
exactly. xml is for a database, behind the hoods, nothing else than text, following some rules about the tags, but for your code, still is just a plain text string.
0
 

Author Closing Comment

by:Alyanto
ID: 39993807
Cheers, soled the problem for me
0

Featured Post

Highfive Gives IT Their Time Back

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

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video discusses moving either the default database or any database to a new volume.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

746 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

12 Experts available now in Live!

Get 1:1 Help Now