Mission 1:通过.NET WebService获取可用数据

这个其实是最基本的实现之一,唯一不爽的是,flex能够自动识别的通常是格式单一整洁的XML,.NET WebService返回的数据集也是XML的格式,不过太过复杂,flex并不能直接从中识别我们真正想要使用的数据,参照网上查找到的做法,我也另外写了一个方法来把数据整理成XML,再以String的方式返回给flex前端,还好,它能看懂。
     public   class  WsUtil
    
{
        
//……

        
public static string BuildXmlFromData(DataSet ds, AttributeFieldPair[] pairs)
        
{
            DataTable dt 
= ds.Tables[0];
            XmlDocument xmlDoc 
= new XmlDocument();
            XmlNode root 
= xmlDoc.CreateElement("node");
            XmlAttribute xa 
= xmlDoc.CreateAttribute("label");
            xa.Value 
= "摘要";
            root.Attributes.Append(xa);
            xmlDoc.AppendChild(root);
            
//
            foreach(DataRow dr in dt.Rows)
            
{
                XmlNode node 
= xmlDoc.CreateElement("node");
                BuildNodeAttributes(
ref node, pairs, dr);
                root.AppendChild(node);
            }

            
//
            return xmlDoc.InnerXml;
        }


        
protected static void BuildNodeAttributes(ref XmlNode node, AttributeFieldPair[] pairs, DataRow dr)
        
{
            
foreach(AttributeFieldPair pair in pairs)
            
{
                XmlAttribute xa 
= node.OwnerDocument.CreateAttribute(pair.AttributeName);
                xa.Value 
= dr[pair.FieldName].ToString();
                node.Attributes.Append(xa);
            }

        }

    }


    
public   struct  AttributeFieldPair
    
{
        
public string AttributeName;
        
public string FieldName;

        
public AttributeFieldPair(string an, string fn)
        
{
            
this.AttributeName = an;
            
this.FieldName = fn;
        }

    }

     public   class  DefaultService : System.Web.Services.WebService
    
{
        
//……

        [WebMethod]
        
public string GetStruct()
        
{
            WsUtil u 
= new WsUtil();
            DataSet ds 
= u.ExecuteQuery("Spector_GetAllProductGroups"new string[]{}new object[]{});
            AttributeFieldPair[] pairs 
= new AttributeFieldPair[1];
            pairs[
0= new AttributeFieldPair("label""Name");
            
string xml = WsUtil.BuildXmlFromData(ds, pairs);
            
return xml;
        }

    }

flex前端把数据直接绑定到tree上:
<? xml version="1.0" encoding="utf-8" ?>
< mx:Application  xmlns:mx ="http://www.adobe.com/2006/mxml"  layout ="absolute"  viewSourceURL ="srcview/index.html"  
    height
="100%"  width ="100%"  pageTitle ="TCost Sample" >
    
    
< mx:Style  source ="CSS/Common.css" ></ mx:Style >
    
< mx:WebService  id ="WS"  wsdl ="http://localhost/TCostWS/DefaultService.asmx?WSDL"
        fault
="mx.controls.Alert.show(event.fault.faultString), 'Error'" >
        
< mx:operation  name ="GetStruct"  resultFormat ="object"  result ="BindStruct(event.result.toString())" >
        
</ mx:operation >
    
</ mx:WebService >

    
< mx:Script >
        
<![CDATA[
            import mx.controls.Alert;
            
            function BindStruct(result:String):void
            {
                var doc:XMLDocument = new XMLDocument(result);
                tree1.dataProvider = doc;
                mx.controls.Alert("done");
            }
        
]]>
    
</ mx:Script >

    
< mx:VBox  width ="100%"  height ="99%"  paddingLeft ="10"  paddingRight ="10" >
        
< mx:HBox  verticalAlign ="bottom"  width ="100%" >
            
< mx:Label  text ="TCost"  fontSize ="32"  paddingLeft ="20" />
            
< mx:Label  id ="lblUser"  text ="早上好,Spector"  paddingLeft ="20" />
            
< mx:HBox  verticalAlign ="bottom"  width ="100%"  horizontalAlign ="right" >
                
< mx:ToggleButtonBar >
                    
< mx:dataProvider >
                        
< mx:Array >
                            
< mx:String > 按月度汇总 </ mx:String >
                            
< mx:String > 按季度汇总 </ mx:String >
                        
</ mx:Array >
                    
</ mx:dataProvider >
                
</ mx:ToggleButtonBar >
            
</ mx:HBox >
        
</ mx:HBox >
        
< mx:HDividedBox  width ="100%"  height ="100%"  dividerThickness ="2" >
            
< mx:Panel  width ="20%"  height ="100%"  title ="用户视角" >
                
< mx:Accordion  width ="100%"  height ="100%"  id ="accordion1" >
                    
< mx:VBox  label ="BU/产品族"  width ="100%"  height ="100%" >
                        
< mx:Tree  width ="100%"  height ="100%"  enabled ="true"  id ="tree1"  dataProvider ="{WS.GetStruct()}"
                            labelField
="@label"  borderThickness ="0" ></ mx:Tree >
                    
</ mx:VBox >
                
</ mx:Accordion >
            
</ mx:Panel >
            
< mx:TitleWindow  height ="100%"  width ="100%"  title ="数据展现" >
                
< mx:Label  text ="windows" />
            
</ mx:TitleWindow >
        
</ mx:HDividedBox >
    
</ mx:VBox >
</ mx:Application >

具体调用的WebService的方法有点奇怪,不是很理解,不过目前先放一放。
需要注意的是,描述URL使用机器名还是localhost是需要统一的,如果我是使用
http://localhost:8700/……
这个路径的话,那页面里面描述WebService路径的时候也要使用locahost,否则会出现跨域访问的错误。
据说从本机环境迁移到发布环境的话,还是要再次面临跨域访问的问题,不过目前也先放一放。

转载于:https://www.cnblogs.com/spector/archive/2007/12/03/981374.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值