Serializing an eConnect document object

The serialization classes enable you to programmatically create eConnect document objects from your .NET application. However, to submit your document to the eConnect business objects, you must convert the .NET document object to the XML format that the business objects require. To convert a .NET document object to XML, use the .NET XmlSerializer and XmlDocument classes to produce a serialized version of your document.

In .NET, serialization is the process of converting an object into a form that can be persisted or transported. For eConnect, you typically convert your document object to a string. For more information about .NET serialization, refer to the .NET Framework SDK.

The following Visual Basic example shows how to create an eConnect sales invoice object and serializes the sales invoice to an XML file. The serialized information is then used with the CreateTransactionEntity method to create the sales invoice in Dynamic GP. As you review the example, note the following actions:

  • The SerializeSalesOrderObject subroutine uses several eConnect serialization classes to create an eConnect sales order document object. Notice how the two taSopLineIvcInsert objects and the taSopHdrIvcInsert object populate the SOPTransactionType object. Also notice how the SOPTransactionType populates the SOPTransactionType field of the eConnectType document object.
  • The SerializeSalesOrderObject subroutine shows how to use a .NET XmlSerializer, FileStream, and XmlTextWriter to serialize the eConnect document object to a file. The code example writes an XML representation of the sales order document object to the SalesOrder.xml file.
  • Notice how the Main subroutine loads the XML from the SalesOrder.xml file into a .NET XmlDocument object.
  • The example shows how to use the OuterXml property of the XmlDocument to populate a string with the XML for the sales order document.
  • The example shows how to instantiate an an eConnectMethods object and how to use the CreateTransactionEntity method. Notice how the sales order document string is used as a parameter for the CreateTransactionEntity method. The example then uses the CreateTransactionEntity method to create the sales order document in the Dynamic GP company database specified by the connection string.
Imports System
Imports System.Xml
Imports System.Xml.Serialization
Imports System.IO
Imports System.Text
Imports Microsoft.Dynamics.GP.eConnect
Imports Microsoft.Dynamics.GP.eConnect.Serialization
Public Class CreateInvoice
  Shared Sub Main()
        Dim salesInvoice As New CreateInvoice
        Dim salesOrderDocument As String
        Dim sConnectionString As String
        Dim eConCall As New eConnectMethods
        Try
            'Call the SerializeSalesOrderObject subroutine and specify
            'a file name
            salesInvoice.SerializeSalesOrderObject("SalesOrder.xml")
            'Create an XML document object and load it with the XML from the
            'file that the SerializeSalesOrder subroutine created
            Dim xmldoc As New Xml.XmlDocument
            xmldoc.Load("SalesOrder.xml")
            'Convert the XML to a string
            salesOrderDocument = xmldoc.OuterXml
            'Create a connection string to the Microsoft Dynamics GP server
            'Integrated Security is required (Integrated security=SSPI)
            sConnectionString = "data source=localhost;" _
                & "initial catalog=TWO;integrated security=SSPI;" _
                & "persist security info=False; packet size=4096"
            'Create the invoice in Microsoft Dynamics GP
            eConCall.CreateTransactionEntity(sConnectionString, _
                    salesOrderDocument)
        Catch exp As eConnectException
            Console.Write(exp.ToString)
        Catch ex As System.Exception
            Console.Write(ex.ToString)
        Finally
            eConCall.Dispose()
        End Try
    End Sub
    'This subroutine creates an eConnect invoice XML document and
    'writes the XML to a file
  Sub SerializeSalesOrderObject(ByVal filename As String)
            Dim salesOrder As New SOPTransactionType
            Dim salesLine As New taSopLineIvcInsert_ItemsTaSopLineIvcInsert
            Dim salesLine2 As New taSopLineIvcInsert_ItemsTaSopLineIvcInsert
            Dim salesHdr As New taSopHdrIvcInsert
            Dim LineItems(1) As taSopLineIvcInsert_ItemsTaSopLineIvcInsert
        Try
            'Populate the elements of the first invoice line
            With salesLine
                .Address1 = "2345 Main St."
                .CUSTNMBR = "CONTOSOL0001"
                .SOPNUMBE = "INV2001"
                .CITY = "Aurora"
                .SOPTYPE = 3
                .DOCID = "STDINV"
                .QUANTITY = 2
                .ITEMNMBR = "ACCS-CRD-12Wh"
                .ITEMDESC = "Phone Cord - 12' White"
                .UNITPRCE = 10.95
                .XTNDPRCE = 21.9
                .LOCNCODE = "WAREHOUSE"
                .DOCDATE = DateString   'Today
            End With
            'Add the invoice line to the array
            LineItems(0) = salesLine
            'Populate the elements of the second invoice line
            With salesLine2
                .Address1 = "2345 Main St."
                .CUSTNMBR = "CONTOSOL0001"
                .SOPNUMBE = "INV2001"
                .CITY = "Aurora"
                .SOPTYPE = 3
                .DOCID = "STDINV"
                .QUANTITY = 2
                .ITEMNMBR = "ACCS-CRD-25BK"
                .ITEMDESC = "Phone Cord - 25' Black"
                .UNITPRCE = 15.95
                .XTNDPRCE = 31.9
                .LOCNCODE = "WAREHOUSE"
                .DOCDATE = DateString   'Today
            End With
            'Add the invoice line to the array
            LineItems(1) = salesLine2
            'Use the array of invoice lines to populate the transaction types
            'array of line items
            ReDim Preserve salesOrder.taSopLineIvcInsert_Items(1)
            salesOrder.taSopLineIvcInsert_Items = LineItems
            'Populate the elements of the taSopHdrIvcInsert XML node
            With salesHdr
                .SOPTYPE = 3
                .SOPNUMBE = "INV2001"
                .DOCID = "STDINV"
                .BACHNUMB = "eConnect"
                .TAXSCHID = "USASTCITY-6*"
                .FRTSCHID = "USASTCITY-6*"
                .MSCSCHID = "USASTCITY-6*"
                .LOCNCODE = "WAREHOUSE"
                .DOCDATE = DateString 'Today
                .CUSTNMBR = "CONTOSOL0001"
                .CUSTNAME = "Contoso, Ltd."
                .ShipToName = "WAREHOUSE"
                .ADDRESS1 = "2345 Main St."
                .CNTCPRSN = "Joe Healy"
                .FAXNUMBR = "13125550150"
                .CITY = "Aurora"
                .STATE = "IL"
                .ZIPCODE = "65700"
                .COUNTRY = "USA"
                .SUBTOTAL = 53.8
                .DOCAMNT = 53.8
                .USINGHEADERLEVELTAXES = 0
                .PYMTRMID = "Net 30"
            End With
            'Add the header node to the transaction type object
            salesOrder.taSopHdrIvcInsert = salesHdr
            'Create an eConnect document object and populate it with
            'the transaction type object
            Dim eConnect As New eConnectType
            ReDim Preserve eConnect.SOPTransactionType(0)
            eConnect.SOPTransactionType(0) = salesOrder
            'Create a file on the hard disk
            Dim fs As New FileStream(filename, FileMode.Create)
            Dim writer As New XmlTextWriter(fs, New UTF8Encoding)
            'Serialize using the XmlTextWriter to the file
            Dim serializer As New XmlSerializer(GetType (eConnectType))
            serializer.Serialize(writer, eConnect)
            writer.Close()
        Catch ex As System.Exception
            Console.Write(ex.ToString)
        End Try
    End Sub
End Class

If you use the example code to create the SalesOrder.xml file. the file should contain the following XML:

<?xml version="1.0" encoding="utf-8"?>
<eConnect xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <SOPTransactionType>
        <taSopLineIvcInsert_Items>
            <taSopLineIvcInsert>
                <SOPTYPE>3</SOPTYPE>
                <SOPNUMBE>INV2001</SOPNUMBE>
                <CUSTNMBR>CONTOSOL0001</CUSTNMBR>
                <DOCDATE>05-07-2004</DOCDATE>
                <LOCNCODE>WAREHOUSE</LOCNCODE>
                <ITEMNMBR>ACCS-CRD-12WH</ITEMNMBR>
                <UNITPRCE>10.95</UNITPRCE>
                <XTNDPRCE>21.9</XTNDPRCE>
                <QUANTITY>2</QUANTITY>
                <ITEMDESC>Phone Cord - 12' White</ITEMDESC>
                <DOCID>STDINV</DOCID>
                <ADDRESS1>2345 Main St.</ADDRESS1>
                <CITY>Aurora</CITY>
            </taSopLineIvcInsert>
            <taSopLineIvcInsert>
                <SOPTYPE>3</SOPTYPE>
                <SOPNUMBE>INV2001</SOPNUMBE>
                <CUSTNMBR>CONTOSOL0001</CUSTNMBR>
                <DOCDATE>05-07-2004</DOCDATE>
                <LOCNCODE>WAREHOUSE</LOCNCODE>
                <ITEMNMBR>ACCS-CRD-25BK</ITEMNMBR>
                <UNITPRCE>15.95</UNITPRCE>
                <XTNDPRCE>31.9</XTNDPRCE>
                <QUANTITY>2</QUANTITY>
                <ITEMDESC>Phone Cord - 25' Black</ITEMDESC>
                <DOCID>STDINV</DOCID>
                <ADDRESS1>2345 Main St.</ADDRESS1>
                <CITY>Aurora</CITY>
            </taSopLineIvcInsert>
        </taSopLineIvcInsert_Items>
        <taSopHdrIvcInsert>
            <SOPTYPE>3</SOPTYPE>
            <DOCID>STDINV</DOCID>
            <SOPNUMBE>INV2001</ SOPNUMBE>
            <TAXSCHID>USASTCITY-6*</TAXSCHID>
            <FRTSCHID>USASTCITY-6*</FRTSCHID>
            <MSCSCHID>USASTCITY-6*</MSCSCHID>
            <LOCNCODE>WAREHOUSE</LOCNCODE>
            <DOCDATE>05-07-2004</DOCDATE>
            <CUSTNMBR>CONTOSOL0001</CUSTNMBR>
            <CUSTNAME>Contoso, Ltd.</CUSTNAME>
            <ShipToName>WAREHOUSE</ShipToName>
            <ADDRESS1>2345 Main St.</ADDRESS1>
            <CNTCPRSN>Joe Healy</CNTCPRSN>
            <FAXNUMBR>13125550150</FAXNUMBR>
            <CITY>Aurora</CITY>
            <STATE>IL</STATE>
            <ZIPCODE>65700</ZIPCODE>
            <COUNTRY>USA</COUNTRY>
            <SUBTOTAL>53.8</SUBTOTAL>
            <DOCAMNT>53.8</DOCAMNT>
            <BACHNUMB>eConnect</BACHNUMB >
            <PYMTRMID>Net 30</PYMTRMID>
        </taSopHdrIvcInsert>
    </SOPTransactionType>
</eConnect>