Wednesday, June 9, 2010

Flex XML - easy to use!

Today I am working on a project that uses an Adobe Flex application to call a .NET back end web service to call some SSRS reports. For one reason or another, I needed to pass an XML string from the front end app to the back end, and found it was incredibly simple. In one of my Actionscript files of my app, I created an XML type variable and added the parameters for the report. The code looked like this:


var rp:XML = <reportParameters/>;

var service:CrmService = new CrmService();

service.addgetAllCustomersEventListener(customerResult);

service.addEventListener(FaultEvent.FAULT,serviceFaultHandler);



var custreq:GetAllCustomers_request = new GetAllCustomers_request ;

rp.appendChild(<start>{startdt.text}</start>);

rp.appendChild(<end>{enddt.text}</end>);

custreq.reportParameters = rp.toXMLString();

service.getAllCustomers_request_var = custreq;

service.getAllCustomers_send();

As you can see, you dont even need quotes around the XML tags, you can do free-form mixing of the tags with the Actionscript code. The variables appear in the curly braces {}. The method called toXMLString() converts all this into a string that looks like this:

<reportParameters><start>1/12/2010</start><end>3/14/2010</end></reportParameters>

The really exciting part is that the XML object managed the containing <reportParameters> tags so I didnt have to worry about closing it properly.
I really like this because now I do not have to write a lot of tedious code with all the open and closing quotes around each literal string and concatenating them together. I hope someone finds this useful.
I hate writing code where I have to carefully manage all beginning and ending