PDA

View Full Version : How to pass XML from ASP.NET Web Service to Flex ?


fx.barrett
11-13-2008, 06:24 AM
Ok, I've been reading all kind of crap articles on this but couldn't find the thing I am looking for... It seems that everyone is using XmlDocument to create the XML structure and then pass it with a Write to Flash/Flex but that's not what I want...

I wrote a tiny web service which has a function in it, that reads some data from a DB and it should parse the read data to XML and then send it to my Flex app... the problem is the following: i have no idea what type of data should the function return and how can I parse my data with a DataSet and Adapter...

At the moment I have something like this ( which obsviously is not function ):

using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Xml;
using System.Diagnostics;

namespace testApp
{
[WebService(Namespace = "http://wisebisoft.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

public class TestService : System.Web.Services.WebService
{

[WebMethod]
public string getPlayers()
{
string strConn = @"Server=.\sqlexpress; Database=Players; Trusted_Connection=yes; integrated security=yes";
SqlConnection conn = new SqlConnection(strConn);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT * FROM Manufacturer";
da.SelectCommand = cmd;
da.Fill(ds, "Players");

return "no idea what to return";
}
}
}

Obviously the function's return type should be changed but I have no idea into what because C# data types are quite retarded and I couldn't figure out what on earth should the function return...

Anyway, I'd like to somehow create the XML structure from this function but without having to define manually all the stuff with XmlDocument, instead, I'd like to use the auto parsers...

Any help is really appreciated. ;)

fx.barrett
11-13-2008, 07:50 PM
Nevermind, figured it out:

using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data;
using System.Data.SqlClient;
using System.Xml;

namespace testApp
{
[WebService(Namespace = "http://wisebisoft.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

public class TestService : System.Web.Services.WebService
{

[WebMethod]
public string getPlayers()
{
string dbConn = @"Server=.\sqlexpress; Database=Players; Trusted_Connection=yes; integrated security=yes";
string dbQuery = "SELECT * FROM Manufacturer";

SqlConnection conn = new SqlConnection(dbConn);
SqlCommand cmd = new SqlCommand(dbQuery, conn);

SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
da.SelectCommand = cmd;
da.Fill(ds, "Manufacturer");
conn.Close();

return ds.GetXml();
}
}
}

yell0wdart
11-13-2008, 09:21 PM
Looks like you beat me to it just a few minutes before I finally posted. LOL

I'll post this anyway, as it would probably be beneficial to have it here. Especially if somebody is needing to do some additional stuff to their data before returning it, merging data from multiple places, data logging from the web service, etc.


----------------------------------------------------------

Actually now that I'm thinking about it, I'm just starting some contract work today. The guys I'm working with have written a web service to just return a List<T> of structs (or objects) (ie: List<Player>). Since we're dealing with a web service, .NET should serialize the return object as XML.

You might want to play with that:


public struct Player
{
private int playerID = -1;
private string firstName = string.Empty;
private string lastNmae = string.Empty;

public int PlayerID
{
get { return playerID; }
set { playerID = value; }
}

public string FirstName
{
get { return firstName; }
set { firstName = value; }
}

public string LastName
{
get { return lastName; }
set { lastName = value; }
}
}

public List<Player> GetPlayers()
{
// db connection and dataset population stuff here...

List<Player> players = new List<Player>();

foreach (DataRow row in ds.Tables[0].Rows)
{
Player player = new Player();
player.PlayerID = (int)row["player_id"];
player.FirstName = row["first_name"].ToString();
player.LastName = row["last_name"].ToString();
players.Add(player);
}

return players;
}


Basically, this collection of Player objects will be serialized as XML and returned by the web method to Flex. From Flex, you should just be able to create an XML document from that web method call.