Imports System
Imports System.IO
Imports System.Xml
Imports System.Xml.XmlReader



Public Class Form1
    Dim mstrInputCSVFile
    Private Sub ButtonBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonBrowse.Click

        Using ofd As New OpenFileDialog
            ofd.Filter = "All files (*.*)|*.*"
            ofd.Title = "Select File"
            If ofd.ShowDialog() = Windows.Forms.DialogResult.OK Then

                mstrInputCSVFile = ofd.FileName
                txtInputCSVFile.Text = mstrInputCSVFile

            End If
        End Using
    End Sub

    Private Sub ButtonMakeOrder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonMakeOrder.Click
        Dim objFile As StreamReader
        Dim objXml As XmlTextWriter
        Dim strString() As String
        Dim count
        Try
            objFile = File.OpenText(mstrInputCSVFile)
            objXml = New XmlTextWriter("c:\order.xml", System.Text.Encoding.UTF8)
            objXml.WriteStartDocument()
            objXml.WriteStartElement("E-Document")
            count = 0
            'Päis
            While objFile.Peek() > -1
                count = count + 1

                strString = objFile.ReadLine().Split(",")
                If count < 2 Then
                    objXml.WriteStartElement("Header")
                    objXml.WriteStartElement("DateIssued")
                    objXml.WriteString(Microsoft.VisualBasic.Left(strString(0), 10))
                    objXml.WriteEndElement()
                    objXml.WriteStartElement("SenderID")
                    objXml.WriteString(strString(1))
                    objXml.WriteEndElement()
                    objXml.WriteStartElement("ReceiverID")
                    objXml.WriteString(strString(2))
                    objXml.WriteEndElement()
                    objXml.WriteEndElement()
                    objXml.WriteStartElement("Document")
                    objXml.WriteStartElement("DocumentType")
                    objXml.WriteString("invoice")
                    objXml.WriteEndElement()
                    objXml.WriteStartElement("DocumentParties")
                    objXml.WriteStartElement("BuyerParty")
                    objXml.WriteStartElement("GLN")
                    objXml.WriteString(strString(3))
                    objXml.WriteEndElement()
                    objXml.WriteEndElement()
                    objXml.WriteStartElement("SellerParty")
                    objXml.WriteStartElement("GLN")
                    objXml.WriteString(strString(4))
                    objXml.WriteEndElement()
                    objXml.WriteEndElement()
                    objXml.WriteStartElement("InvoiceeParty")
                    objXml.WriteStartElement("GLN")
                    objXml.WriteString(strString(5))
                    objXml.WriteEndElement()
                    objXml.WriteEndElement()
                    objXml.WriteStartElement("DeliveryParty")
                    objXml.WriteStartElement("GLN")
                    objXml.WriteString(strString(6))
                    objXml.WriteEndElement()
                    objXml.WriteStartElement("RegNum")
                    objXml.WriteString(strString(7))
                    objXml.WriteEndElement()
                    objXml.WriteEndElement()
                    objXml.WriteEndElement()
                    objXml.WriteStartElement("DocumentInfo")
                    objXml.WriteStartElement("DocumentName")
                    objXml.WriteString("arve")
                    objXml.WriteEndElement()
                    objXml.WriteStartElement("DocumentNum")
                    objXml.WriteString(strString(8))
                    objXml.WriteEndElement()
                    objXml.WriteStartElement("DateInfo")
                    objXml.WriteStartElement("InvoiceDate")
                    objXml.WriteString(Microsoft.VisualBasic.Left(strString(0), 10))
                    objXml.WriteEndElement()
                    objXml.WriteStartElement("ProcessingDate")
                    objXml.WriteString(strString(0))
                    objXml.WriteEndElement()
                    objXml.WriteStartElement("DueDate")
                    objXml.WriteString(Microsoft.VisualBasic.Left(strString(0), 10))
                    objXml.WriteEndElement()
                    objXml.WriteEndElement()
                    objXml.WriteStartElement("RefInfo")
                    objXml.WriteStartElement("SourceDocument")
                    objXml.WriteStartAttribute("type")
                    objXml.WriteValue("order")
                    objXml.WriteStartElement("SourceDocumentNum")
                    objXml.WriteString(strString(9))
                    objXml.WriteEndElement()
                    objXml.WriteEndElement()
                    objXml.WriteEndElement()
                    objXml.WriteEndElement()
                    objXml.WriteStartElement("DocumentSumGroup")
                    objXml.WriteStartElement("DocumentSum")
                    objXml.WriteString(strString(10))
                    objXml.WriteEndElement()
                    objXml.WriteStartElement("VAT")
                    objXml.WriteStartElement("SumBeforeVAT")
                    objXml.WriteString(strString(10))
                    objXml.WriteEndElement()
                    objXml.WriteStartElement("VATRate")
                    objXml.WriteString(strString(11))
                    objXml.WriteEndElement()
                    objXml.WriteStartElement("VATSum")
                    objXml.WriteString(strString(12))
                    objXml.WriteEndElement()
                    objXml.WriteEndElement()
                    objXml.WriteStartElement("TotalSum")
                    objXml.WriteString(strString(13))
                    objXml.WriteEndElement()
                    objXml.WriteEndElement()

                End If

                If count > 1 Then
                    objXml.WriteStartElement("DocumentItem")
                    objXml.WriteStartElement("ItemEntry")
                    objXml.WriteStartElement("LineItemNum")
                    objXml.WriteString(strString(0))
                    objXml.WriteEndElement()
                    objXml.WriteStartElement("ItemDescription")
                    objXml.WriteString(strString(1))
                    objXml.WriteEndElement()
                    objXml.WriteStartElement("SellerItemCode")
                    objXml.WriteString(strString(2))
                    objXml.WriteEndElement()
                    objXml.WriteStartElement("GTIN")
                    objXml.WriteString(strString(3))
                    objXml.WriteEndElement()
                    objXml.WriteStartElement("ItemUnitEntry")
                    objXml.WriteStartElement("ItemUnit")
                    objXml.WriteString("tk")
                    objXml.WriteEndElement()
                    objXml.WriteStartElement("ItemPrice")
                    objXml.WriteString(strString(4))
                    objXml.WriteEndElement()
                    objXml.WriteEndElement()
                    objXml.WriteStartElement("BaseUnit")
                    objXml.WriteString("tk")
                    objXml.WriteEndElement()
                    objXml.WriteStartElement("AmountInvoiced")
                    objXml.WriteString(strString(5))
                    objXml.WriteEndElement()
                    objXml.WriteStartElement("ItemPrice")
                    objXml.WriteString(strString(4))
                    objXml.WriteEndElement()
                    objXml.WriteStartElement("ItemSum")
                    objXml.WriteString(strString(6))
                    objXml.WriteEndElement()
                    objXml.WriteStartElement("VAT")
                    objXml.WriteStartElement("SumBeforeVAT")
                    objXml.WriteString(strString(6))
                    objXml.WriteEndElement()
                    objXml.WriteStartElement("VATRate")
                    objXml.WriteString(strString(7))
                    objXml.WriteEndElement()
                    objXml.WriteStartElement("VATSum")
                    objXml.WriteString(strString(8))
                    objXml.WriteEndElement()
                    objXml.WriteEndElement()
                    objXml.WriteStartElement("ItemTotal")
                    objXml.WriteString(strString(9))
                    objXml.WriteEndElement()
                    objXml.WriteEndElement()
                    objXml.WriteEndElement()

                End If
            End While
            objXml.WriteEndElement()
            objXml.WriteEndDocument()
            MsgBox("Ok")
        Catch ex As Exception
        Finally
            If Not objXml Is Nothing Then
                objXml.Close()
                objXml = Nothing
            End If
            If Not objFile Is Nothing Then
                objFile.Close()
                objFile = Nothing
            End If
        End Try

    End Sub
End Class


