SerializerUtil.cs
Código PHP:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
namespace WebTest
{
/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
[Serializable()]
public abstract class SerializerUtil<T>
{
/// <summary>
/// Initializes a new instance of the <see cref="SerializerUtil<T>"/> class.
/// </summary>
public SerializerUtil()
{
}
/// <summary>
/// Serializes this instance.
/// </summary>
/// <returns></returns>
public string Serialize()
{
XmlSerializer serializer = new XmlSerializer(this.GetType());
using (StringWriter stream = new StringWriter())
{
serializer.Serialize(stream, this);
stream.Flush();
return stream.ToString();
}
}
/// <summary>
/// Deserializes the specified XML.
/// </summary>
/// <param name="xml">The XML.</param>
/// <returns></returns>
public static T Deserialize(string xml)
{
if (string.IsNullOrEmpty(xml))
{
throw new ArgumentNullException("xml");
}
XmlSerializer serializer = new XmlSerializer(typeof(T));
using (StringReader stream = new StringReader(xml))
{
try
{
return (T)serializer.Deserialize(stream);
}
catch (Exception ex)
{
throw new InvalidOperationException("Failed to create object from xml string", ex);
}
}
}
/// <summary>
/// Processes this instance.
/// </summary>
/// <returns></returns>
public abstract string Process();
}
}
WebService.asmx y WebService.asmx.cs
Código PHP:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace WebTest
{
/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService
{
[WebMethod]
public string ProcessData(string serializedXML)
{
WebTest.User myUser = SerializerUtil<WebTest.User>.Deserialize(serializedXML);
return myUser.UserID.ToString();
}
}
}
Default.aspx
Código PHP:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebTest._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="sendDataButton" runat="server" onclick="sendDataButton_Click"
Text="Send data" />
<asp:Literal ID="userIdLiteral" runat="server" />
</div>
</form>
</body>
</html>
Default.aspx.cs
Código PHP:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebTest
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void sendDataButton_Click(object sender, EventArgs e)
{
WebTest.User myUser = new User();
myUser.UserID = 999;
myUser.UserName = "Stream";
myUser.Age = 28;
string result = myUser.Process();
this.userIdLiteral.Text = string.Format("El userID es: {0}", result);
}
}
}
Ahi esta, la idea es simple, hagan una pagina, con un boton, el boton envia los datos serializados de un objeto a un servicio web, el servicio web recibe el xml serializado y convierte el xml a un objeto y regresen el id, luego, desde la pagina impriman el mensaje: "El userID es:" + el valor
En el codigo anterior, creo un objeto de tipo user, le asigno valores lo envio a un servicio web serializandolo:
Código PHP:
<?xml version="1.0" encoding="utf-16"?>
<User xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<UserID>999</UserID>
<UserName>Stream</UserName>
<Age>28</Age>
</User>
y mi servicio web lo recibe asi:
Código PHP:
<?xml version="1.0" encoding="utf-16"?>
<User xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<UserID>999</UserID>
<UserName>Stream</UserName>
<Age>28</Age>
</User>
El cual regresa el valor 999..
A ver.. Hagan lo mismo en php, estoy ancioso por verlo...