Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 662
  • Last Modified:

SOAP, .NET and converting / [de]serializing date and time data from XmlElement.InnerText

I'm working on a simple data loader for SalesForce.com. It's written in VB.NET as a simple Windows Forms application and talks to SalesForce through a Web Service.

It's similar to working with a traditional .NET database in many ways except that rows and columns of data are returned in arrays of XmlElements. Being new to SOAP, Web Services and SalesForce, I'm confused about how to handle the data returned from querying the SF database.

To get at the raw data I can use XmlElement.InnerText. This works well for text / string fields but I'm not sure what I should be doing with more complicated data types like date, datetime and time. Do I have to convert them from text into a .NET Date field myself or is there something in .NET somewhere that will do this automatically?

Currently I'm handling the conversion in my code. I determine the data type of an XmlElement by looking up its .Name in a schema and then perform the necessary conversion.

In the pseudo code below, I'm converting all of the columns in a row (an array of XmlElements) into an array of variants (objects) called vCols.

For ColumnIndex = 0 To NumberOfColumns - 1

      ' SalesForce's Api stores the array of XmlElements that represent the row of data in a field called "Any"
      
      Dim e As Xml.XmlElement = Record.Any(ColumnIndex)
      
      Select Case GetSoapType(e.Name)

      Case soapType.xsdboolean
            vCols(c) = e.InnerText = "true"

      Case soapType.xsddate
            vCols(c) = DateTime.ParseExact(e.InnerText, "yyyy-MM-dd", Nothing)

      Case soapType.xsddateTime
            vCols(c) = DateTime.ParseExact(e.InnerText, "yyyy-MM-ddTHH:mm:ss.fffZ", Nothing)

      Case soapType.xsddouble
            vCols(c) = jwToDouble(e.InnerText) ' wrapped CDouble()

      Case soapType.xsdint
            vCols(c) = jwToInt(e.InnerText) ' wrapped CInt()

      Case soapType.xsdstring
            vCols(c) = e.InnerText

      Case soapType.xsdtime
            vCols(c) = DateTime.ParseExact(e.InnerText, "HH:mm:ss.fffZ", Nothing)

      Case SfPartnerApi.soapType.tnsID
            vCols(c) = e.InnerText

      Case SfPartnerApi.soapType.xsdanyType ' not sure what to do with this data type... haven't come across it yet
            vCols(c) = e.InnerText

      Case SfPartnerApi.soapType.xsdbase64Binary ' not sure what to do with this data type
            vCols(c) = e.InnerText

      End Select

Next
      
Is this the correct approach to handling data returned as arrays of XmlElements or should I be using specialized .NET classes to do this sort of thing? I have to go the other way as well (convert dates and times from the .NET DateTime format to text in an XmlElement) so I'd like to make sure I'm not making a mountain out of a mole-hill.
0
ou81aswell
Asked:
ou81aswell
1 Solution
 
Meir RivkinFull stack Software EngineerCommented:
unfortunately, u can't avoid having this kind of code to convert from different data types.
same thing happens when u need to convert DB types to .net types and vice versa.
my only suggestion to you is having this code in a separate class which accepts SoapType and return converted value.
something like:
Public Class SoapTypeConverter
    Public Shared Function Convert(e As XmlElement) As Object
        Dim res As Object

        Select Case GetSoapType(e.Name)

            Case soapType.xsdboolean
                res = e.InnerText = "true"

            Case soapType.xsddate
                res = DateTime.ParseExact(e.InnerText, "yyyy-MM-dd", Nothing)

            Case soapType.xsddateTime
                res = DateTime.ParseExact(e.InnerText, "yyyy-MM-ddTHH:mm:ss.fffZ", Nothing)

            Case soapType.xsddouble
                res = jwToDouble(e.InnerText) ' wrapped CDouble()

            Case soapType.xsdint
                res = jwToInt(e.InnerText) ' wrapped CInt()

            Case soapType.xsdstring
                res = e.InnerText

            Case soapType.xsdtime
                res = DateTime.ParseExact(e.InnerText, "HH:mm:ss.fffZ", Nothing)

            Case SfPartnerApi.soapType.tnsID
                res = e.InnerText

            Case SfPartnerApi.soapType.xsdanyType ' not sure what to do with this data type... haven't come across it yet
                res = e.InnerText

            Case SfPartnerApi.soapType.xsdbase64Binary ' not sure what to do with this data type
                res = e.InnerText

        End Select
        Return res
    End Function
End Class

Open in new window

0
 
ou81aswellAuthor Commented:
Thanks. I have implemented the conversion code in one of my classes.
0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now