I am working on a console program to generate an XML file using a SQL Server database and .NET.
Currently, I am stuck with my code to generate a proper XML file. As my Categories node are not properly identified as XML format when I open the file in Notepad and as a result, am unable to consume it well.
What is needed is this:
Generate a proper XML tags for Categories as below & shouldn't include any special characters when opening in any notepad.
If I have multiple site ID (locations), how I can do a for loop from my SQL table against my code.
Dim dsXmlDoc As XmlDocument = New XmlDocument()
ds.Tables(0).TableName = "Inv"
dsXmlDoc.LoadXml(ds.GetXml())
Dim xmlInner As XmlElement = CType(xmlDoc.SelectSingleNode("/Root/"),
XmlElement)xmlInner.AppendChild(xmlDoc.ImportNode(dsXmlDoc.DocumentElement, True))
I think it'll be this:
WITH x AS (
SELECT
A.[ORDER ID],
RIGHT(A.[POS ID], 2) AS [LesseePOSNumber],
REPLACE(A.[BUSINESS DATE],'-','') + '-' + A.[RECEIPT NUMBER] AS [InvoiceNumber],
'1' AS [DocumentType],
CAST(A.[BUSINESS DATE] AS datetime) + CAST(A.[START SALE TIME] AS DATETIME) AS [TransactionDateTime],
SUM(A.ITEM) AS [TotalNumberOfItems],
SUM(A.[TOTAL AMOUNT] - A.[TOTAL TAX]) AS [TransactionTotalWithoutVAT],
SUM(A.[TOTAL TAX]) AS [VATTotalAmount],
'1' AS [PaymentType],
'4' AS [Currency]
FROM
(
SELECT
T1.[storeId] AS [STORE NO]
,CONVERT(date,CONVERT(varchar(15),T1.businessDate,101)) AS [BUSINESS DATE]
,FORMAT(CONVERT(datetime,SUBSTRING(T1.startSaleTime,1,2) + ':' + SUBSTRING(T1.startSaleTime,3,2) + ':' + SUBSTRING(T1.startSaleTime,5,2), 121), 'h:mm:ss') AS [START SALE TIME]
,CASE WHEN (T1.Type) = 'TRX_Refund' OR (T1.Type) = 'TRX_Overring' THEN CAST(T1.[totalAmount] AS decimal(16,2)) * -1 ELSE CAST(T1.[totalAmount] AS decimal(16,2)) END AS [TOTAL AMOUNT]
,CASE WHEN (T1.Type) = 'TRX_Refund' OR (T1.Type) = 'TRX_Overring' THEN 0 ELSE 1 END AS [GC]
,CASE WHEN (T1.Type) = 'TRX_Refund' OR (T1.Type) = 'TRX_Overring' THEN CAST(T1.[totalTax] AS decimal(16,2)) * -1 ELSE CAST(T1.[totalTax] AS decimal(16,2)) END AS [TOTAL TAX]
,T1.NodeId AS [POS ID]
,T1.[receiptNumber] AS [RECEIPT NUMBER]
,T1.orderId AS [ORDER ID]
,
(
SELECT SUM(CASE WHEN CAST(T2.totalPrice AS float) > 0.00 THEN 1 ELSE 0 END)
FROM [McDonalds_STG].[dbo].[ITEMLEVEL] T2
WHERE T2.orderId = T1.orderId
) AS [ITEM]
FROM [McDonalds_STG].[dbo].[Orders] T1
) A
GROUP BY
A.[STORE NO],
A.[BUSINESS DATE],
A.[POS ID],
A.[RECEIPT NUMBER],
A.[START SALE TIME],
A.[ORDER ID]
)
SELECT
x.[LesseePOSNumber],
x.[InvoiceNumber],
x.[DocumentType],
x.[TransactionDateTime],
x.[TotalNumberOfItems],
x.[TransactionTotalWithoutVAT],
x.[VATTotalAmount],
x.[PaymentType],
x.[Currency],
(
SELECT
49 AS [ID],
(CAST(T1.[totalAmount] AS decimal(16,2)) - CAST(T1.[totalTax] AS decimal(16,2)) ) AS [Total]
FROM [Order] T2
WHERE T2.orderId = x.[ORDER ID]
FOR XML PATH('Category'), TYPE
) As Categories
FROM x
FOR XML PATH ('Invoice'), root ('Invoices')
There are still some opportunities here for significant, sensible refactoring here (the date/string/date handling needs throwing away and doing again without using strings where possible), and it might need some typos fixing; it's not an easy SQL to read and I don't expect it'll just paste straight into SSMS and work without some tweaks!
Note that you GROUP BY STORE NO but you don't appear to use it
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With