Deserializing a Transaction Requester document
This section of the documentation shows how to convert the string returned by the GetEntity method of the eConnectMethods class to an eConnect serialization object that you can use in your .NET development project. In .NET, the process of converting a string to an object is called deserialization. To deserialize the eConnect XML to an object from the eConnect Serialization classes, you use the following eConnect and .NET components.
To retrieve eConnect data as an XML string, use the GetEntity method of the eConnectMethods class. The GetEntity method uses the eConnect Transaction Requester to retrieve data and create an XML string.
Warning: The eConnect Transaction Requester supports a limited number of eConnect document types. Always check whether the Transaction Requester can retrieve the document type that you want to use. Also check that the Transaction Requester returns the data fields you need to create a valid object.
You use .NET StringReaders and XmlTextReaders to reformat the XML string you receive from the GetEntity method. Typically, you use the data fields from the Transaction Requester XML string to populate the data fields of an XML string that represents a serialized eConnect document type.
Use the Deserialize method of the .NET XmlSeralizer to create an instance of an eConnect serialization class. The Deserialize method converts the XML string into an eConnect serialization object you can use with your .NET project.
To illustrate the deserialization procedure, the following steps show how to retrieve customer information as an XML string and how to convert that string into a taUpdateCreateCustomerRcd object.
Use GetEntity to retrieve a record
Use the GetEntity method to retrieve an XML string that includes the data fields for the specified Transaction Requester document.
To view an example of how to use the GetEntity method to retrieve a customer record, see Retrieving XML documents with GetEntity.
The following Visual Basic example shows how to use the GetEntity method to obtain a customer record. Notice how the return result populates the customerDoc string.
'Retrieve the customer document Dim customerDoc = eConnectMethods.GetEntity(connectionString, xmldoc.OuterXml)
Retrieve the data fields for the record
Use a .NET StringReader and XmlTextReader to retrieve the data fields from the Transaction Requester XML string. Create a new XML string that contains the data fields.
The following Visual Basic example retrieves the field values contained in the customerDoc string. Notice how customerDoc is loaded into the StringReader and XmlTextReader objects. Also notice how the XmlTextReader uses the name of the Customer node in customerDoc to identify the parent node of the data fields. The ReadInnerXml method of the XmlTextReader returns a string that contains all the data fields that were retrieved by the Transaction Requester.
'**Retrieve the customer XML** 'Load the customer document string into a StringReader Dim requestReader As New StringReader(customerDoc) 'Use the StringReader to populate an XML text reader Dim xmlTextReader As New XmlTextReader(requestReader) 'Use the XML text reader to find the customer XML in the eConnect ' Requester response document 'The eConnect_Out_Setup table shows that the customer XML data will ' be enclosed in <Customer> tags xmlTextReader.ReadToFollowing("Customer") Dim customerXml = xmlTextReader.ReadInnerXml()
Create a serialized eConnect object
Create a string that represents a serialized version of the object you want to create. For example, to create a taUpdateCreateCustomerRcd you create a string that includes the XML for the customer object.
The following Visual Basic example shows how to create a string that represents a serialized taUpdateCreateCustomerRcd object. Notice how the customerXml string supplies the XML for the data fields. The customerObjectXml string is then added to a .NET StringReader object.
'Create a string that places the customer XML into an taUpdateCreateCustomerRcd XML node Dim customerObjectXml = String.Concat("<?xml version=""1.0""?> _ <taUpdateCreateCustomerRcd>", customerXml, _ "</taUpdateCreateCustomerRcd>") 'Use a StringReader to read the XML for the taUpdateCreateCustomerRcd XML node Dim customerReader As New StringReader(customerObjectXml)
Deserialize the eConnect XML string
Use your XML string with the Deserialize method of the .NET XmlSerializer to create an instance of an eConnect serialization object. The Deserialize method uses the XML in the string to populate the fields of the object.
The following Visual Basic example shows how to use a .NET XmlSerializer to deserialize the customerObjectXml in the customerReader object. Notice how CType is used to specify the type of the object.
'**Deserialize the taUpdateCreateCustomerRcd XML node from the StringReader** Dim deSerializer As New XmlSerializer(GetType(taUpdateCreateCustomerRcd)) 'Cast the deserialized object to a taUpdateCreateCustomerRcd ' serialization object Dim testObject = CType(deSerializer.Deserialize(customerReader), _ taUpdateCreateCustomerRcd)