Solved

Inserting XML into SQL Server Table

Posted on 2014-04-09
4
260 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
[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
  • 2
  • 2
4 Comments
 
LVL 143

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
 
LVL 1

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 143

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
 
LVL 1

Author Closing Comment

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

Featured Post

Create CentOS 7 Newton Packstack Running Keystone

A bug was filed against RDO for the installation of Keystone v3. This guide is designed to walk you through the configuration for using Keystone v3 with Packstack. You will accomplish this using various repos and the Answers file.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

634 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