Solved

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

Posted on 2013-06-10
2
641 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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Salesforce.com is a cloud-based customer relationship management (CRM) system. In this article, you will learn how to add and map custom lead and contact fields to your Salesforce instance.
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

680 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