?
Solved

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

Posted on 2013-06-10
2
Medium Priority
?
652 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 42

Accepted Solution

by:
sedgwick earned 2000 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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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 …

770 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