Return Array Collections to Flex using Web Services and .NET

One of the benefits of using AMF with Flex is that data comes back very clean and ready to use in your Flex applications with ActionScript. Web services, however, don’t always have that advantage, because they wrap everything in a SOAP envelope, and many time requires code to extract XML, etc. to convert data into a usable format on the Flex side.

Format your WSDL correctly in your web service, and Flex will interpret the SOAP and XML into Array Collections that contain objects that represent your data.

At lease one way to perform this successfully using Flex and .NET web services is to use the XmlDocument class on the .NET side. Generally you’ll get your data from a database, loop over the record set and create an XML version of the dataset, and return it to Flex via a web service. Rather than returning the XML dataset as string, return it as a native XML document as an XmlDocument which is a .NET (at leaset C#) object. Flex will interpret the SOAP and XML returned as an ArrayCollection. It pretty simple.

Example:

C# code to get data from the database, convert the dataset into an xml string, and load that xml into an XmlDocument and return it to Flex

[WebMethod]
public XmlDocument GetClientsXML()
{
XmlDocument clientXMLDoc = new XmlDocument();
//create the connection to the database
SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["FOO"].ConnectionString);
SqlCommand cmd = new SqlCommand("spGetClients",conn);
cmd.CommandType = CommandType.StoredProcedure;
StringWriter sw = new StringWriter();
XmlTextWriter xw = new XmlTextWriter(sw);
SqlDataReader dr;</code>

//get the records
try
{
conn.Open();
dr = cmd.ExecuteReader();
//loop over the data set and create the xml for the clients
if(dr.HasRows)
{
xw.WriteStartElement("clients");
while(dr.Read())
{
xw.WriteStartElement("client");
xw.WriteStartElement("clientId");
xw.WriteString(((Int16) dr["ClientId"]).ToString());
xw.WriteEndElement();
xw.WriteStartElement("firstName");
xw.WriteString(dr["FirstName"].ToString());
xw.WriteEndElement();
xw.WriteStartElement("lastName");
xw.WriteString(dr["LastName"].ToString());
xw.WriteEndElement();
xw.WriteStartElement("email");
xw.WriteString(dr["Email"].ToString());
xw.WriteEndElement();
xw.WriteStartElement("city");
xw.WriteString(dr["City"].ToString());
xw.WriteEndElement();
xw.WriteStartElement("State");
xw.WriteString(dr["State"].ToString());
xw.WriteEndElement();
xw.WriteStartElement("zip");
xw.WriteString(dr["Zip"].ToString());
xw.WriteEndElement();
xw.WriteEndElement();
}
xw.WriteEndElement();
}

}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
conn.Close();
}
//parse the xml by loading it into the clientXMLDoc var and then return it as a native XML document
clientXMLDoc.LoadXml(xw.ToString());
return clientXMLDoc;
}

ActionScript code to extract the ArrayCollection from the web service result

public function result(event:ResultEvent):void
{
//now drill down into the soap object to get an array collection
model.clientListAC = event.result.clients.client as ArrayCollection;</code>

//notice that you need to traverse the result to find where it is in the SOAP/XML structure
var objClients:Object = event.result; //var to show how to drill into the soap object not used for anything else
}

The best way to inspect your return result is to use the Flex Debugger or Service Capture to view the SOAP and XML.

soaparraycollection.pngservicecapturesoap.png


About this entry