With the tools .NET provides the developer, enabling other XML applications from a Web Service is pretty easy, and together with an application like Flash 5, can really be a nice aid in functionality. While this article focuses more on how to get access to the XML stream sent from a Web Service, it will also give a brief introduction to how Flash works with XML. When viewing the example at the end of the article make sure you have Player 5 or Player 6.
The example updates a Flash 5 movie which shows a user a "Sample Document" where they can click and view Parts 1-3 and the Introduction of the document. The titles and text will be updated from our Web Service. First let's set up a table in our database and our asmx file.
Create a table called FlashUpdate in your database with the following structure:
CREATE TABLE [FlashUpdate] (
[documenttitle] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[part1header] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[part2header] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[part3header] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[part1text] [text] COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[part2text] [text] COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[part3text] [text] COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[documentintro] [text] COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
|
We have 8 fields which hold values for the main document title, part 1-3 document titles, part 1-3 text, and the document introduction (or default) text. You can then enter in some text of your own (it might be a good idea to keep the text short for testing purposes).
Now let's setup our Web Service file called flash_service.asmx which is:
<%@ WebService Language="c#" Class="MyDataService" %>
using System;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;
[WebService(Namespace="http://www.superdotnet.com/flash_service/",
Description="A Web Service To Retrieve Data From A Database",
Name="MyDataServices")]
public class MyDataService : WebService
{
[WebMethod(BufferResponse=true,
CacheDuration=0,
Description="This method retrieves all data from our database",
MessageName="GetData")]
public DataSet GetData()
{
String DataSource = "server=;database=mydb;uid=;pwd=;";
SqlConnection conn = new SqlConnection(DataSource);
String sql = "Select * FROM FlashUpdate";
conn.Open();
SqlDataAdapter db_sqladaptor = new SqlDataAdapter(sql,conn);
DataSet ds = new DataSet();
db_sqladaptor.Fill(ds,"MyDataResult");
return ds;
}
}
|
Our Web Service has one method GetData which just grabs all the data from the table FlashUpdate and returns the DataSet. That's all we need to do with the .NET code, and create it like any other XML Web Service file to be consumed.