Introduction
This article introduces GTK# development with MonoDevelop for consuming simple web services. The web service we are going to use is the Stock Ticker service available at http://www.webservicex.net/stockquote.asmx?wsdl.
Background
This project is developed using MonoDevelop and GTK# 2.0. I have developed and tested this application in KDE 3.5 and GNOME with OpenSUSE 11.
Creating a Web Reference
First, we need to create a Web Reference to the Stock ticker service in the form of a DLL file so that we can consume it. Here are the steps for the same.
- Convert the WSDL of the Web Service in C# Code using wsdl tool.
- Generate a Library file using the generated .cs file.
Open your terminal and type the following commands:
$ wsdl www.webservicex.net/stockquote.asmx?wsdl
$ mcs /target:library StockQuote.cs -r System.Web.Services
A DLL file with the name StockQuote.dll will be created. Though we can do this using the MonoDevelop IDE, I prefer this technique because we could store all web references in one special folder and reference them as required in projects.
Create a new Solution in MonoDevelop. Add a project of the type GTK# 2.0.
Then, the project will create a GTK# form with the name MainWindow.cs. You can open this in Designer mode which shows up a WYSIWYG editor. One major feature of GTK# for design is that it asks you to first define a Container before you get to add any Widgets.
I have used a table as my container to design the form as shown below:
The StockQuote web service returns the result in an XML format. So we need to extract information from it. You can see the format here. Just lookup a company, say MSFT and see the format of result. Therefore, we need to add two References, one is for System.Xml and the other is for the StockQuote.dll we generated earlier.
References can be added easily by Right clicking the References folder in Solution window and selecting Edit References... This will open a dialog box as shown below:
Next, we add the reference to StockQuote.dll by selecting the .NET Assembly Tab.
In GTK#, a Widget is associated with Signals. Signals associated with a widget can be seen in the Properties window when we select in the designer. We can double click on the Signal to generate event handler. Next, we add signal handler to the Button "Get latest stock information" and write the following code:
protected virtual void OnButton1Clicked (object sender, System.EventArgs e)
{
String quoteSymbol="MSFT";
if(radOwnSym.Active){
if(txtOwn.Text=="")
return;
quoteSymbol=txtOwn.Text;
}
if(radPreDef.Active){
quoteSymbol=cmbTemplate.ActiveText;
}
StockQuote quote = new StockQuote();
string toParse=(quote.GetQuote(quoteSymbol));
XmlDocument doc=new XmlDocument();
doc.LoadXml(toParse);
Console.WriteLine(toParse);
lblLast.Text=doc.FirstChild.FirstChild.ChildNodes[1].InnerXml;
lblStockSymbol.Text=doc.FirstChild.FirstChild.ChildNodes[0].InnerXml;
}
We can then build the project by pressing F8. The project can be run by pressing the F5 key.
Points of Interest
The code for the designer refused to show up the IDs of the widgets that I dragged and dropped. I got toppled in the first place. It actually shows the widget IDs when you compile the project after dropping the widgets.
History
- v0.1b Retrieving detailed stock information of a given stock symbol