2009-11-06

Using LINQ to retrieve XML data

Although it is like "to use a cannon to kill a mosquito", I find using LINQ to read XML has a clear advantage - to read the node like an associative array (like PHP).

I use the following VB example to illustrate:


Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim xml_str As String
xml_str = "<ROOT><FIELD1>VALUE1</FIELD1><FIELD2>VALUE2</FIELD2><FIELD3>VALUE3</FIELD3></ROOT>"
Dim xmltree As System.Xml.Linq.XElement
xmltree = XElement.Load(New System.IO.StringReader(xml_str))
Label1.Text = xmltree.Element("FIELD2").Value
End Sub
End Class



I deliberately use a string to hold the XML content in this example, although the native constructor can be as simple as

Dim xmlTree1 As XElement =
<Root>
 <Child1>1</Child1>
 <Child2>2</Child2>
 <Child3>3</Child3>
</Root>

2009-11-03

Using VB 2008 Express as client to consume SugarCRM Web service

In additional to my previous example of using AXIS as web client, I now use VB.NET as another example to consume web service. Moreover, I will just the open source SugarCRM as web service provider.

SugarCRM
The wsdl link is as follows (the host name depends on your installation. I use localhost here)
http://127.0.0.1/sugarcrm/soap.php?wsdl
as shown in the following IE screen dump


VB
The set up is a little bit tricky, as the initial menu does not have any web service item. The steps are as follows:
(1) Project > Add Service Reference


(2) Click the "Advanced" button (Do not type the WDSL link yet)


(3) Click the "Add Web Reference" button


(4) Type the WSDL link and press "Go" button. The web service description will be displayed. Then choose a reference name you like (I use SugarCRM) and press "Add Reference"


(5) You will find VB has added the WS reference in the Solution Explorer


(6) Ironically, VB will now have the "Add Web Referebce" menu item available in the Project menu


7) Now you can start to write codes to consume the web service. Since my example just illustrate the principle, I have hard-coded many things. The logic is there is a Button1. If I click this button, it will call the login method of SugarCRM. The session id returned will be displayed as Label1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim soapservice As New SugarCRM.sugarsoap
Dim credential As New SugarCRM.user_auth
Dim opensession As New SugarCRM.set_entry_result
credential.user_name = "admin"
credential.password = getMd5Hash("password")
credential.version = "1.0"
opensession = soapservice.login(credential, "SugarCRM")
Label1.Text = opensession.id
End Sub


The convenience of using VB is its IntelliSense which can enumerate the available options.


I have declared three objects
soapservice - the client stub
credential - the first argument of the service, containing the user id, md5 of the password and the version.
openssion - the returned result of the service. One of the most important field is the session ID

The result is shown below