4 Replies Latest reply on Mar 27, 2008 2:06 PM by ntsiii

    bind xml to a datagrid

    eexposito
      Trying to bind some xml to a datagrid from a .Net web service call but haven't had much luck.

      "Attach Code"
      The .Net Service Method

      [WebMethod]
      public string HelloWorld()
      {
      StringBuilder sTable = new StringBuilder();

      sTable.Append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
      sTable.Append("<NewDataSet>");
      sTable.Append("<Table>");
      sTable.Append("<CITY>Lawrence</CITY>");
      sTable.Append("<STATE>KS</STATE>");
      sTable.Append("</Table>");
      sTable.Append("<Table>");
      sTable.Append("<CITY>Miami</CITY>");
      sTable.Append("<STATE>FL</STATE>");
      sTable.Append("</Table>");
      sTable.Append("</NewDataSet>");

      return sTable.ToString();
      }

      The Flex App:

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" width="600" height="450">
      <mx:Script>
      <![CDATA[
      import mx.controls.Alert;
      import mx.rpc.events.ResultEvent;
      import mx.rpc.events.FaultEvent;
      import mx.collections.ArrayCollection;
      import mx.managers.CursorManager;
      import mx.utils.ArrayUtil;

      private namespace nspace = " http://tempuri.org/";
      use namespace nspace;
      ]]>
      </mx:Script>
      <mx:WebService id="TestApp" wsdl=" http://localhost/FlexService.asmx?WSDL" showBusyCursor="true" fault="Alert.show(event.fault.faultString), 'Error'" useProxy="false">
      <mx:operation name="HelloWorld" resultFormat="e4x">

      </mx:operation>
      </mx:WebService>
      <mx:VBox>
      <mx:Button click="TestApp.HelloWorld.send()" ></mx:Button>
      <mx:DataGrid dataProvider="{TestApp.HelloWorld.lastResult.HelloWorldResult.NewDataSet.Table}" width="100%">
      <mx:columns>
      <mx:DataGridColumn dataField="CITY" headerText="city"/>
      <mx:DataGridColumn dataField="STATE" headerText="state"/>
      </mx:columns>
      </mx:DataGrid>
      <mx:TextArea width="400" height="100">
      <mx:text>
      {TestApp.HelloWorld.lastResult.toXMLString()}
      </mx:text>
      </mx:TextArea>
      </mx:VBox>
      </mx:Application>

      The text area shows the following xml when I run it:

      <HelloWorldResponse xmlns=" http://tempuri.org/" xmlns:soap=" http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd=" http://www.w3.org/2001/XMLSchema">
      <HelloWorldResult>&lt;?xml version="1.0" encoding="utf-8" ?&gt;&lt;NewDataSet&gt;&lt;Table&gt;&lt;CITY&gt;Lawrence&lt;/CITY&gt;&lt;STATE&gt;KS&lt;/ STATE&gt;&lt;/Table&gt;&lt;Table&gt;&lt;CITY&gt;Miami&lt;/CITY&gt;&lt;STATE&gt;FL&lt;/STAT E&gt;&lt;/Table&gt;&lt;/NewDataSet&gt;</HelloWorldResult>
      </HelloWorldResponse>

      Any help would be greatly appreciated.
        • 1. Re: bind xml to a datagrid
          ntsiii Level 3
          You look like you are building a dataset as an xml string, and sending the string to Flex. I am not confident that will work.

          I do this by building a DataTable using the DataTable API, and returning the DataTable itself to flex.

          This results in some fairly complex xml, but in my result handler, I find the "DocumentElement" node, and that is the root of my data. Works fine for me.

          Tracy
          • 2. Re: bind xml to a datagrid
            eexposito Level 1
            Hi Tracy, thanks for the reply. Could you please provide some sample code on how you consume the DataTable using the DocumentElement node.

            Thanks,
            EE
            • 3. Re: bind xml to a datagrid
              ntsiii Level 3
              Sure. I really regret that the forum will trash the code formatting, but hopefully we will have an attach Code function soon.

              Here is my event handler, the first 20 lines anyway.

              /**
              * The handleResult() method is the generic result event handler
              */
              private function handleResult(event:ResultEvent):void
              {
              var atCall:AsyncToken;
              var sWSMethod:String;
              var xmlResult:XML;
              if(event.result != null) {
              atCall = event.token;
              sWSMethod = atCall.wsMethod;

              xmlResult = XML(event.result)..DocumentElement[0];

              if (xmlResult && xmlResult.children()[0].name() == "ERROR") {
              _xmlLastFault = xmlResult;
              Alert.show(_xmlLastFault.toXMLString(),"Data Service Error");
              this.dispatchEvent(new Event("DSFaultEvent"));
              }
              else {
              trace("DSApp.handleResult: " + sWSMethod);
              trace(xmlResult.toXMLString())
              switch (sWSMethod) {
              /** Master.asmx methods */
              case "getContacts":
              xmlContacts = xmlResult;
              ...
              • 4. Re: bind xml to a datagrid
                ntsiii Level 3
                Note: what I do on the server side is catch any data errors and build an "ERROR" DataTable, and send that back. If the first child of the DocumentElement is <ERROR>, I handle that, otherwise I just process the data normally.

                Tracy