Solved

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

Posted on 2013-06-10
2
634 Views
Last Modified: 2016-02-17
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
Comment
Question by:ou81aswell
2 Comments
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 39237402
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
 

Author Closing Comment

by:ou81aswell
ID: 39239819
Thanks. I have implemented the conversion code in one of my classes.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

To properly understand GitHub, let’s divide it into two words ‘Git’ and ‘Hub’. Git is basically a ‘Distribution Version Control’ (DVC) and ‘Source Code Management’ (SCM) system widely used by software programmers while Hub means the efficient centre…
Note: You must have administrative privileges in order to configure lead or case queues. Salesforce.com (http://www.Salesforce.com) is a cloud-based customer relationship management (CRM) system. It is widely used around the world by sales and ma…
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now