1 Reply Latest reply on May 3, 2007 8:33 AM by CAI_LT

    Namespace Datagrid Binding Issue

    CAI_LT
      Hi,

      My web service result is not binding correctly to a datagrid. The resulting e4x document’s attributes will bind correctly but the elements will not. I am using an ASP.Net web service but it is returning object arrays that serialize correctly to the expected XML format.

      XML document received from the web service as obtained from Flex Builder debugger:

      <Get_DataResponse xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd=" http://www.w3.org/2001/XMLSchema" xmlns:soap=" http://schemas.xmlsoap.org/soap/envelope/" xmlns=" http://Localhost/sample">
      <Get_DataResult>
      <series>
      <row a="1">
      <b>
      "10"
      </b>
      <c>
      "11"
      </c>
      </row>
      <row a="2">
      <b>
      "20"
      </b>
      <c>
      "21"
      </c>
      </row>
      </series>
      </Get_DataResult>
      </Get_DataResponse>

      Code I am using to display the result:

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute">
      <mx:Script>
      <![CDATA[

      import mx.controls.Alert;
      import mx.rpc.events.ResultEvent;
      import mx.rpc.events.FaultEvent;
      import mx.rpc.soap.LoadEvent;
      import mx.collections.ArrayCollection;
      import mx.managers.CursorManager;
      import mx.utils.ArrayUtil;

      default xml namespace = " http://Localhost/sample";

      ]]>
      </mx:Script>

      <mx:WebService id="WS" wsdl=" http://Localhost/Sample/Service.asmx?wsdl" useProxy="false" fault="Alert.show(event.fault.faultString), &apos;Error&apos;" showBusyCursor="true">
      <mx:operation name="Get_Data" resultFormat="e4x" makeObjectsBindable="true">
      <mx:request>
      <num>10</num>
      </mx:request>
      </mx:operation>
      </mx:WebService>

      <mx:Canvas right="0" left="0" top="0" bottom="0">
      <mx:VBox top="0" left="0" right="0" bottom="0">
      <mx:Button label="Click to Run" click="WS.Get_Data.send()"/>
      <mx:HDividedBox width="100%" height="100%">

      <mx:DataGrid height="100%" fontSize="12" dataProvider="{WS.Get_Data.lastResult.Get_DataResult.series.row}" width="100%">
      <mx:columns>
      <mx:DataGridColumn dataField="@a" headerText="a"/>
      <mx:DataGridColumn dataField="b" headerText="b"/>
      <mx:DataGridColumn dataField="c" headerText="c"/>
      </mx:columns>
      </mx:DataGrid>

      </mx:HDividedBox>
      </mx:VBox>
      </mx:Canvas>

      </mx:Application>



      I can bind an XML object to the datagrid correctly if I run the result through the following code to remove the namespaces:

      private function WSResult(event:ResultEvent):void
      {
      var myPattern:RegExp = /<Get_DataResponse.*?>/;

      var xml:String = event.result.toString();

      myXML = XML(xml.replace(myPattern, "<Get_DataResponse>"));
      }

      I saw where other people are having the same issue and are using the datagrid label function to populate the grid.

      Is this by design or it an issue with the data binding?

      Thank you for your help.
        • 1. Re: Namespace Datagrid Binding Issue
          CAI_LT Level 1
          With further research this does not appear to be a web service issue but rather a namespace issue. The following code demonstrates the problem I am having with the attributes of a XML document binding correctly while the elements are not.

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute">
          <mx:Script>
          <![CDATA[

          import mx.controls.Alert;
          import mx.collections.ArrayCollection;
          import mx.managers.CursorManager;
          import mx.utils.ArrayUtil;

          default xml namespace = " http://Localhost/sample";

          ]]>
          </mx:Script>

          <mx:XML id="myXML">
          <Get_DataResponse xmlns=" http://Localhost/sample">
          <Get_DataResult>
          <series>
          <row a="1">
          <b>10</b>
          <c>11</c>
          </row>
          <row a="2">
          <b>20</b>
          <c>21</c>
          </row>
          </series>
          </Get_DataResult>
          </Get_DataResponse>
          </mx:XML>

          <mx:Canvas right="0" left="0" top="0" bottom="0">
          <mx:VBox width="100%" height="100%">
          <mx:DataGrid height="100%" fontSize="12" dataProvider="{myXML.Get_DataResult.series.row}" width="100%">
          <mx:columns>
          <mx:DataGridColumn dataField="@a" headerText="a"/>
          <mx:DataGridColumn dataField="b" headerText="b"/>
          <mx:DataGridColumn dataField="c" headerText="c"/>
          </mx:columns>
          </mx:DataGrid>

          </mx:VBox>
          </mx:Canvas>

          </mx:Application>



          If the namespace is removed from the XML and code the binding works as expected. The following code demonstrates this:



          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute">
          <mx:Script>
          <![CDATA[
          import mx.controls.Alert;
          import mx.collections.ArrayCollection;
          import mx.managers.CursorManager;
          import mx.utils.ArrayUtil;
          ]]>
          </mx:Script>

          <mx:XML id="myXML">
          <Get_DataResponse>
          <Get_DataResult>
          <series>
          <row a="1">
          <b>10</b>
          <c>11</c>
          </row>
          <row a="2">
          <b>20</b>
          <c>21</c>
          </row>
          </series>
          </Get_DataResult>
          </Get_DataResponse>
          </mx:XML>
          <mx:Canvas right="0" left="0" top="0" bottom="0">
          <mx:VBox width="100%" height="100%">
          <mx:DataGrid height="100%" fontSize="12" dataProvider="{myXML.Get_DataResult.series.row}" width="100%">
          <mx:columns>
          <mx:DataGridColumn dataField="@a" headerText="a"/>
          <mx:DataGridColumn dataField="b" headerText="b"/>
          <mx:DataGridColumn dataField="c" headerText="c"/>
          </mx:columns>
          </mx:DataGrid>
          </mx:VBox>
          </mx:Canvas>
          </mx:Application>

          Is this working as expected or do I need to change the way I am binding to the XML?

          Thank you for your help.