Set the resultFormat on the WebService or operation to "e4x".  Unless
you do this Flex convert you xml to a stucture of nested dynamic
objects. You almost never want this.

 

Then in your handler, inspect your data:

retData = event.result as XML;

trace(retData.toXMLString());  //so yo can know exactly how to build the
expressin in the next line
retList = retData.data;

trace(retList.toXMLString());  //is this what you expect?

 

Tracy

________________________________

From: [email protected] [mailto:[EMAIL PROTECTED] On
Behalf Of duanstravels
Sent: Thursday, November 06, 2008 1:53 PM
To: [email protected]
Subject: [flexcoders] Using XML Object passed from ASP.NET web service
in a Flex Datagrid

 

Hey Guys

I have literally been banging my head on this for the last three days. 
I have used flex in a previous project, and had enormous success with 
the great charts and presentation layer. I am now taking on a fairly 
ambitious new project and wanted to do things a different way, as it 
needs to be scalable.

My previous project used HTTPServices to connect to a .aspx page, 
which in turn returned a XML page. This works, but is very messy.

I would now like to use webservices, as I think it would simplify 
things a great deal.

I have written the following service in ASP.NET, which works as it 
returns the XML document when I test the method by running the 
report.asmx file from the following link:

http://localhost:1464/MyProject/report.asmx/All_Entities
<http://localhost:1464/MyProject/report.asmx/All_Entities> 

***********************************************************
WebMethods and function in ASP.NET Web service:

<WebMethod()> _
Public Function HelloWorld() As String
Return "This works"
End Function

<WebMethod()> _
Public Function All_Entities() As XmlDocument
Return ReturnXMLData("sp_AllEntities", 0, "NA")
End Function

Public Function ReturnXMLData(ByVal spName As String, _
ByVal numParams As Integer, _
ByVal ParamArray spParam() As String) 
As XmlDocument

Dim cnString As String = 
ConfigurationManager.ConnectionStrings("cnDBConnect").ConnectionString

Dim myConnection As New SqlConnection
myConnection.ConnectionString = cnString

Dim myCommand As New SqlCommand
With myCommand
.Connection = myConnection
.CommandType = CommandType.StoredProcedure
.CommandText = spName
End With

myConnection.Open()

'Return the XML data from SQL and read into a string value
Dim xmlr As System.Xml.XmlReader
xmlr = myCommand.ExecuteXmlReader()
xmlr.Read()

Dim retXMLString As New StringBuilder
retXMLString.Append("<data>")
Do While xmlr.ReadState <> ReadState.EndOfFile
retXMLString.Append(xmlr.ReadOuterXml())
Loop
retXMLString.Append("</data>")
myConnection.Close()

Dim xmlDoc As New XmlDocument()
xmlDoc.LoadXml(retXMLString.ToString())
Return xmlDoc

End Function

***********************************************************
The XML output returned from the .NET web service is below. I copied 
this straight from the page returned inside Firefox after calling the 
webmethod:

<data>
<root EntityName="US" EntityId="1"/>
<root EntityName="EMEA" EntityId="2"/>
<root EntityName="Benelux" EntityId="3"/>
<root EntityName="Africa" EntityId="4"/>
<root EntityName="Asia" EntityId="5"/>
</data>
***********************************************************

I now need to catch the output (which I assume is a XML object) and 
then bind it to a datagrid or chart.

I have the following MXML component set up. It connects to the 
webservice, catches the result, and then converts it into XML. I then 
try to bind it to a datagrid. I also have a text box which binds to a 
method "HelloWorld()" which returns a string value and then correctly 
displays the text, so I know the connection works:

***********************************************************
<?xml version="1.0" encoding="utf-8"?>
<mx:Box xmlns:mx="http://www.adobe.com/2006/mxml
<http://www.adobe.com/2006/mxml> " width="100%" 
height="100%" >

<mx:Script>
<![CDATA[
import mx.rpc.events.ResultEvent; 
[Bindable]
private var retData:XML;
private var retList:XMLList;
private var lstAM:ArrayCollection;

private function handleResult(event:ResultEvent):void{

retData = event.result as XML;
retList = retData.data;
this.dgDataReturned.dataProvider = retList;
}

]]>
</mx:Script>

<mx:Button label="Get String" click="{WS.HelloWorld();}"/>
<mx:TextArea width="252" height="116" id="txtHello" 
text="{WS.HelloWorld.lastResult}"/>

<mx:Button label="Get Data" click="{WS.All_Entities();}"/>
<mx:DataGrid id="dgDataReturned">
<mx:columns>
<mx:DataGridColumn headerText="label" 
dataField="@EntityName"/>
<mx:DataGridColumn headerText="data" 
dataField="@EntityId"/>
</mx:columns>
</mx:DataGrid>

<mx:WebService id="WS" 
wsdl="http://metrix3/asp/metrixwebservice.asmx?wsdl
<http://metrix3/asp/metrixwebservice.asmx?wsdl> "
result="handleResult(event);">
</mx:WebService>

</mx:Box>

***********************************************************

I have tried just about anything to get this working, but as I have 
learned flex as I needed I don't really have the background knowledge 
to even ask the right questions when searching for answers.

I am not sure if there are other people struggling with this as well, 
but I think it must be a common problem. I just haven't been able to 
find any help online.

Any ideas would be appreciated.

Cheers
Duan

 

Reply via email to