//Asp.net端代码
//getxml.aspx代码,保留一行即可,删除其他的html代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="getxml.aspx.cs" Inherits="getxml" %>
//getxml.aspx.cs
//using System...
using System.Xml;
public partial class getxml : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string user_pkid = System.Web.HttpContext.Current.Request.Form["user_pkid"];
if user_pkid != null)
{
CreateXml();//创建Xml的方法,可使用XmlTextWriter、XmlDocument ,或者直接读取Xml文件等待
}
}
private void CreateXml()
{
XmlDocument doc = new XmlDocument();
XmlNode root = doc.CreateElement("channel");
XmlElement titleElm = doc.CreateElement("title");
titleElm.InnerText = "blogweather";
//...
root.AppendChild(titleElm);
doc.AppendChild(root);
XmlTextWriter xw = new XmlTextWriter(Response.OutputStream,System.Text.Encoding.UTF8);//写到页面返回值中
xw.Formatting = Formatting.Indented;//将Xml格式化
doc.Save(xw);
xw.Flush();
xw.Close();
}
}
Xml数据如下:
<?xml version="1.0" encoding="UTF-8" ?>
<channel>
<title>blogweather</title>
<link>http://www.blogweather.net</link>
<description>博客天气预报</description>
</channel>
方法一:
如果所有值均在xml数据中,而且不需要拿这些数据做二次分析,则推荐使用 HTTPService控件
Flex 端代码:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" initialize="init()" >
<mx:Script>
<![CDATA[
import mx.messaging.AbstractConsumer;
import flash.events.MouseEvent;
import mx.controls.Alert;
private function init():void
{
getxml.url = "http://www.blogweather.net/getxml.aspx"; //接收Post方法的页面
var data:Object = new Object();
data["user_pkid"] = this.parameters.user_pkid;
getxml.send(data);
}
]]>
</mx:Script>
<mx:HTTPService id="getxml" showBusyCursor="true" useProxy="false" method="POST">
</mx:HTTPService>
<mx:TextArea wordWrap="true" editable="false" enabled="true" id="lb_title">
<mx:text>{getxml.lastResult.channel.title}</mx:text>
</mx:TextArea>
</mx:Application>
方法二:
如果要将数据进行分析,则要使用URLLoader和URLRequest
Flex 端代码:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" initialize="init();">
<mx:Script>
<![CDATA[
import mx.messaging.AbstractConsumer;
import mx.messaging.channels.StreamingAMFChannel;
import flash.events.MouseEvent;
import mx.controls.Alert;
public var myLoader:URLLoader = new URLLoader();
public var myRequest:URLRequest;
public var user_pkid:String;
private function init():void
{
var http://www.cnblogs.com/glaivelee/admin/String = "http://www.blogweather.net/getxml.aspx";
myRequest = new URLRequest(url);
myRequest.method = URLRequestMethod.POST;
var data:URLVariables = new URLVariables();
//接收来自flash的参数调用,比如flash文件为 loadxml.swf,带参数 loadxml.swf?user_pkid=10001
data.user_pkid = this.parameters.user_pkid; // 获取10001
myRequest.data = data;
myLoader.load(myRequest);
myLoader.addEventListener(Event.COMPLETE,onLoadComplete);
}
private function onLoadComplete(event:Event):void
{
var myxml:XML;
var loader:URLLoader = URLLoader(event.target);
myxml = new XML(loader.data);
lb_title.text =myxml.child("channel")[0].child("title");
if( lb_title.text == "blogweather")
{
Alert("页面名称为:博客天气预报");
}
}
]]>
</mx:Script>
<mx:TextArea wordWrap="true" editable="false" enabled="true" id="lb_title">
<mx:text>lb_title</mx:text>
</mx:TextArea>
</mx:Application>