Web Service Loop Through XML and Insert into MSSQL Tables

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi
Ive written the below VB that captures the below xml and inserts most of it into a SQL table.<br/>
The <lines> tag however contains multiple <line>âs and if I use the same approach as below I only get one record in my other SQL table.<br/>
From reading around I think I need to build the result into an array and then loop through it. However I canât figure out where to start. Please help?<br/>
Many thanks

XML
<?xml version="1.0" encoding="UTF-8"?><br/>
<Orders><br/>
<Order><br/>
<CustomerAddress><br/>
<ContactName>Bob Builder</ContactName><br/>
<AddressLine1>163 Sandhurst Close</AddressLine1><br/>
<AddressLine2>Stoke Lodge</AddressLine2><br/>
<AddressLine3>London</AddressLine3><br/>
<AddressLine4>Over There</AddressLine4><br/>
<PostCode>BJ69 6AA</PostCode><br/>
<CountryCode>GB</CountryCode><br/>
<EmailAddress>x3mc5n5g3tycc5b@here.co.uk</EmailAddress><br/>
<TelephoneNumber>014566412760</TelephoneNumber><br/>
</CustomerAddress><br/>
<DeliveryAddress><br/>
<ContactName>Bob Builder</ContactName><br/>
<AddressLine1>163 Sandhurst Close</AddressLine1><br/>
<AddressLine2>Stoke Lodge</AddressLine2><br/>
<AddressLine3>London</AddressLine3><br/>
<AddressLine4>Over There</AddressLine4><br/>
<PostCode>BJ69 6AA</PostCode><br/>
<CountryCode>GB</CountryCode><br/>
</DeliveryAddress><br/>
<TransactionReference>026-6631795-6534732</TransactionReference><br/>
<TransactionDate>17/11/2011</TransactionDate><br/>
<Channel>Web</Channel><br/>
<Currency>GBP</Currency><br/>
<VAT>0</VAT><br/>
<DespatchService>Standard</DespatchService><br/>
<DespatchCharge>2.8</DespatchCharge><br/>
<Charges>0</Charges><br/>
<Discount>0</Discount><br/>
<SpecialInstructions><br/>
</SpecialInstructions><br/>
<Lines><br/>
<Line><br/>
<Quantity>1</Quantity><br/>
<ProductCode>9781402774720</ProductCode><br/>
<Price>9.99</Price><br/>
</Line><br/>
<Line><br/>
<Quantity>1</Quantity><br/>
<ProductCode>9781402774720</ProductCode><br/>
<Price>9.99</Price><br/>
</Line><br/>
</Lines><br/>
<Transactions><br/>
<Transaction><br/>
<PaymentMethod>web</PaymentMethod><br/>
<PaymentTotal>12.79</PaymentTotal><br/>
</Transaction><br/>
</Transactions><br/>
</Order><br/>
</Orders>

Web Service (VB)
Imports System.Web.Services<br/>
Imports System.Web.Services.Protocols<br/>
Imports System.ComponentModel<br/>
Imports System<br/>
Imports System.Xml<br/>
Imports System.Xml.Serialization<br/>
Imports System.IO<br/>
Imports Microsoft.VisualBasic<br/>
Imports System.Data.SqlClient
<System.Web.Services.WebService(Namespace:=" http://webservice.meemee.me:8620 http://webservice.meemee.me:8620 ")> _<br/>
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _<br/>
<ToolboxItem(False)> _<br/>
Public Class Service1<br/>
Inherits System.Web.Services.WebService
Public Structure Order
Public CA() As CA<br/>
End Structure
<br/>
Public Structure CA
Public CustomerAddress As CustomerAddress<br/>
Public DeliveryAddress As DeliveryAddress<br/>
Public TransactionReference As String<br/>
Public TransactionDate As String<br/>
Public Channel As String<br/>
Public Currency As String<br/>
Public VAT As String<br/>
Public DespatchService As String<br/>
Public DespatchCharge As String<br/>
Public Charges As String<br/>
Public Discount As String<br/>
Public SpecialInstructions As String<br/>
Public Lines As Lines<br/>
Public Transactions As Transactions
<br/>
End Structure<br/>
Public Class CustomerAddress<br/>
Public ContactName As String<br/>
Public AddressLine1 As String<br/>
Public AddressLine2 As String<br/>
Public AddressLine3 As String<br/>
Public AddressLine4 As String<br/>
Public PostCode As String<br/>
Public CountryCode As String<br/>
Public EmailAddress As String<br/>
Public TelephoneNumber As String<br/>
End Class
Public Class DeliveryAddress<br/>
Public ContactName As String<br/>
Public AddressLine1 As String<br/>
Public AddressLine2 As String<br/>
Public AddressLine3 As String<br/>
Public AddressLine4 As String<br/>
Public PostCode As String<br/>
Public CountryCode As String<br/>
End Class<br/>
Public Class Lines<br/>
Public Line As Line
End Class<br/>
Public Class Line<br/>
Public Quantity As String<br/>
Public ProductCode As String<br/>
Public Price As String<br/>
End Class<br/>
Public Class Transactions<br/>
Public Transaction As Transaction<br/>
End Class<br/>
Public Class Transaction<br/>
Public PaymentMethod As String<br/>
Public PaymentTotal As String<br/>
End Class
<br/>
<WebMethod()> _<br/>
Public Function Orders(Order As CA) As String<br/>
Try<br/>
Dim SQLCon As New SqlClient.SqlConnection<br/>
Dim SQLCmd As New SqlClient.SqlCommand<br/>
SQLCon2.ConnectionString = "Data Source=srver;User ID=####;Password=#####;"<br/>
SQLCon.Open()<br/>
SQLCmd.CommandText = " Insert Into AA_E_Header (CustomerAddress_ContactName,CustomerAddress_AddressLine1,CustomerAddress_AddressLine2,CustomerAddress_AddressLine3,CustomerAddress_AddressLine4,CustomerAddress_PostCode,CustomerAddress_CountryCode,CustomerAddress_EmailAddress,CustomerAddress_TelephoneNumber,DeliveryAddress_ContactName,DeliveryAddress_AddressLine1,DeliveryAddress_AddressLine2,DeliveryAddress_AddressLine3,DeliveryAddress_AddressLine4,DeliveryAddress_PostCode,DeliveryAddress_CountryCode,TransactionReference,TransactionDate,Channel,Currency,VAT,DespatchService,DespatchCharge,Charges,Discount,SpecialInstructions)
" _<br/>
& " Select @PM_1, @PM_2, @PM_3, @PM_4, @PM_5, @PM_6, @PM_7,
@PM_8, @PM_9, @PM_10, @PM_11, @PM_12, @PM_13, @PM_14, @PM_15, @PM_16, @PM_17, @PM_18, @PM_19, @PM_20, @PM_21, @PM_22, @PM_23, @PM_24,@PM_25,@PM_26"<br/>
Dim PM_1 As New SqlParameter()<br/>
Dim PM_2 As New SqlParameter()<br/>
Dim PM_3 As New SqlParameter()<br/>
Dim PM_4 As New SqlParameter()<br/>
Dim PM_5 As New SqlParameter()<br/>
Dim PM_6 As New SqlParameter()<br/>
Dim PM_7 As New SqlParameter()<br/>
Dim PM_8 As New SqlParameter()<br/>
Dim PM_9 As New SqlParameter()<br/>
Dim PM_10 As New SqlParameter()<br/>
Dim PM_11 As New SqlParameter()<br/>
Dim PM_12 As New SqlParameter()<br/>
Dim PM_13 As New SqlParameter()<br/>
Dim PM_14 As New SqlParameter()<br/>
Dim PM_15 As New SqlParameter()<br/>
Dim PM_16 As New SqlParameter()<br/>
Dim PM_17 As New SqlParameter()<br/>
Dim PM_18 As New SqlParameter()<br/>
Dim PM_19 As New SqlParameter()<br/>
Dim PM_20 As New SqlParameter()<br/>
Dim PM_21 As New SqlParameter()<br/>
Dim PM_22 As New SqlParameter()<br/>
Dim PM_23 As New SqlParameter()<br/>
Dim PM_24 As New SqlParameter()<br/>
Dim PM_25 As New SqlParameter()<br/>
Dim PM_26 As New SqlParameter()
PM_1.ParameterName = "@PM_1"<br/>
PM_2.ParameterName = "@PM_2"<br/>
PM_3.ParameterName = "@PM_3"<br/>
PM_4.ParameterName = "@PM_4"<br/>
PM_5.ParameterName = "@PM_5"<br/>
PM_6.ParameterName = "@PM_6"<br/>
PM_7.ParameterName = "@PM_7"<br/>
PM_8.ParameterName = "@PM_8"<br/>
PM_9.ParameterName = "@PM_9"<br/>
PM_10.ParameterName = "@PM_10"<br/>
PM_11.ParameterName = "@PM_11"<br/>
PM_12.ParameterName = "@PM_12"<br/>
PM_13.ParameterName = "@PM_13"<br/>
PM_14.ParameterName = "@PM_14"<br/>
PM_15.ParameterName = "@PM_15"<br/>
PM_16.ParameterName = "@PM_16"<br/>
PM_17.ParameterName = "@PM_17"<br/>
PM_18.ParameterName = "@PM_18"<br/>
PM_19.ParameterName = "@PM_19"<br/>
PM_20.ParameterName = "@PM_20"<br/>
PM_21.ParameterName = "@PM_21"<br/>
PM_22.ParameterName = "@PM_22"<br/>
PM_23.ParameterName = "@PM_23"<br/>
PM_24.ParameterName = "@PM_24"<br/>
PM_25.ParameterName = "@PM_25"<br/>
PM_26.ParameterName = "@PM_26"
PM_1.Value = Order.CustomerAddress.ContactName<br/>
PM_2.Value = Order.CustomerAddress.AddressLine1<br/>
PM_3.Value = Order.CustomerAddress.AddressLine2<br/>
PM_4.Value = Order.CustomerAddress.AddressLine3<br/>
PM_5.Value = Order.CustomerAddress.AddressLine4<br/>
PM_6.Value = Order.CustomerAddress.PostCode<br/>
PM_7.Value = Order.CustomerAddress.CountryCode<br/>
PM_8.Value = Order.CustomerAddress.EmailAddress<br/>
PM_9.Value = Order.CustomerAddress.TelephoneNumber<br/>
PM_10.Value = Order.DeliveryAddress.ContactName<br/>
PM_11.Value = Order.DeliveryAddress.AddressLine1<br/>
PM_12.Value = Order.DeliveryAddress.AddressLine2<br/>
PM_13.Value = Order.DeliveryAddress.AddressLine3<br/>
PM_14.Value = Order.DeliveryAddress.AddressLine4<br/>
PM_15.Value = Order.DeliveryAddress.PostCode<br/>
PM_16.Value = Order.DeliveryAddress.CountryCode<br/>
PM_17.Value = Order.TransactionReference<br/>
PM_18.Value = Order.TransactionDate<br/>
PM_19.Value = Order.Channel<br/>
PM_20.Value = Order.Currency<br/>
PM_21.Value = Order.VAT<br/>
PM_22.Value = Order.DespatchService<br/>
PM_23.Value = Order.DespatchCharge<br/>
PM_24.Value = Order.Charges<br/>
PM_25.Value = Order.Discount<br/>
PM_26.Value = Order.SpecialInstructions

SQLCmd.Parameters.Add(PM_1)<br/>
SQLCmd.Parameters.Add(PM_2)<br/>
SQLCmd.Parameters.Add(PM_3)<br/>
SQLCmd.Parameters.Add(PM_4)<br/>
SQLCmd.Parameters.Add(PM_5)<br/>
SQLCmd.Parameters.Add(PM_6)<br/>
SQLCmd.Parameters.Add(PM_7)<br/>
SQLCmd.Parameters.Add(PM_8)<br/>
SQLCmd.Parameters.Add(PM_9)<br/>
SQLCmd.Parameters.Add(PM_10)<br/>
SQLCmd.Parameters.Add(PM_11)<br/>
SQLCmd.Parameters.Add(PM_12)<br/>
SQLCmd.Parameters.Add(PM_13)<br/>
SQLCmd.Parameters.Add(PM_14)<br/>
SQLCmd.Parameters.Add(PM_15)<br/>
SQLCmd.Parameters.Add(PM_16)<br/>
SQLCmd.Parameters.Add(PM_17)<br/>
SQLCmd.Parameters.Add(PM_18)<br/>
SQLCmd.Parameters.Add(PM_19)<br/>
SQLCmd.Parameters.Add(PM_20)<br/>
SQLCmd.Parameters.Add(PM_21)<br/>
SQLCmd.Parameters.Add(PM_22)<br/>
SQLCmd.Parameters.Add(PM_23)<br/>
SQLCmd.Parameters.Add(PM_24)<br/>
SQLCmd.Parameters.Add(PM_25)<br/>
SQLCmd.Parameters.Add(PM_26)<br/>
SQLCmd.Connection = SQLCon<br/>
SQLCmd.ExecuteNonQuery()<br/>
SQLCmd.Connection.Close()<br/>
SQLCmd.Parameters.Clear()<br/>
SQLCon.Close()

<br/>
Dim SQLCon2 As New SqlClient.SqlConnection<br/>
Dim SQLCmd2 As New SqlClient.SqlCommand<br/>
SQLCon2.ConnectionString = "Data Source=srver;User ID=####;Password=#####;"<br/>
SQLCon2.Open()<br/>
SQLCmd2.CommandText = "Insert Into AA_E_Details ( Product_Code,Price , Quantity) Select @PM_Lines_1, @PM_Lines_2,@PM_Lines_3 "
Dim PM_Lines_1 As New SqlParameter()<br/>
Dim PM_Lines_2 As New SqlParameter()<br/>
Dim PM_Lines_3 As New SqlParameter()
PM_Lines_1.ParameterName = "@PM_Lines_1"<br/>
PM_Lines_2.ParameterName = "@PM_Lines_2"<br/>
PM_Lines_3.ParameterName = "@PM_Lines_3"
PM_Lines_1.Value = Order.Lines.Line.ProductCode<br/>
PM_Lines_2.Value = Order.Lines.Line.Price<br/>
PM_Lines_3.Value = Order.Lines.Line.Quantity
SQLCmd2.Parameters.Add(PM_Lines_1)<br/>
SQLCmd2.Parameters.Add(PM_Lines_2)<br/>
SQLCmd2.Parameters.Add(PM_Lines_3)
SQLCmd2.Connection = SQLCon2<br/>
SQLCmd2.ExecuteNonQuery()<br/>
SQLCmd2.Connection.Close()<br/>
SQLCmd2.Parameters.Clear()<br/>
SQLCon2.Close()

Return "Order " & Order.TransactionReference & " Accepted"

Catch ex As Exception<br/>
Return "Error: " + ex.ToString<br/>
End Try
End Function

<br/>
End Class





View the full article
 
Back
Top