Principales respuestas
WebService C# mostrar formato XML o Soap.

Pregunta
-
Buenos días estoy trabajando con Webservice por primera vez y me están pidiendo que cuando se ejecute me muestre en formato XML o Soap, creo que es en etiquetas, pero nose como hacerlo, ayuden porfa, a mi me sale así, este el código que estoy usando.
string response = cli.UploadString("http://sandbox-api.boxeway.com/api/pickup//booking", "POST", data);
yo obtengo, el Code, message y content, como pueden ver en la imagen.
y me muestra esto:
This XML file does not appear to have any style information associated with it. The document tree is shown below.<string xmlns="http://tempuri.org/">{"code":0,"message":"ok","content":"204"}</string>
Yo quiero que me muestre así:
<Rpta>ok</Rpta>
<Cod.Operacion>204</Cod.Operacion>
se los agradecería, es urgente por favor.
- Editado WALTER BRENIS lunes, 11 de septiembre de 2017 17:22
Respuestas
-
hola
pero estas retornando como respuesta el json que obtienes de la invocacion por WebClient, si quieres otra respues deberas transformar los datos
deberias tomar la respuesta del json y convertir a otro objeto diferente
public static string SerializeObject<T>(T toSerialize) { XmlSerializer xmlSerializer = new XmlSerializer(toSerialize.GetType()); using(StringWriter textWriter = new StringWriter()) { xmlSerializer.Serialize(textWriter, toSerialize); return textWriter.ToString(); } }
entonces defines la clase
public class Reserva{ public string Rpta {get;set;} public string CodOperacion{get;set;} }
para luego poder convertir
string response = cli.UploadString("http://sandbox-api.boxeway.com/api/pickup//booking", "POST", data); XmlDocument doc = JsonConvert.DeserializeXmlNode(response, "rootNodeName"); var result = new Reserva(){ Rpta = doc.Node("xx").Value, CodOperacion = doc.Node("xx").Value } return SerializeObject<Reserva>(result);
pero retornas la serializacion de tu clase y no la que obtuviste de la llamada
saludos
Leandro Tuttini
Blog
MVP Profile
Buenos Aires
Argentina- Propuesto como respuesta Jorge TurradoMVP martes, 12 de septiembre de 2017 7:34
- Marcado como respuesta Moderador MModerator lunes, 18 de septiembre de 2017 16:11
Todas las respuestas
-
Hola WALTER BRENIS
Lo que pasa es que estás utilizando JsonConvert, comenta la línea en donde realizas eso y recupera solo como un string:
//XmlDocument ... = JsonConver
nos dices ¿qué ocurre al colocarla como comentario? . Por otro lado, no confundas los conceptos XML o Json son formatos en los que te arrojará los datos. SOAP es un protocolo: Simple Object Access Protocol
Si ayudé a resolver tu consulta, no olvides marcar como respuesta y/o votar como útil.
-
ya comenté y me sale lo mismo:
nosé algun codigo para darle el formatoo algo???
This XML file does not appear to have any style information associated with it. The document tree is shown below.<string xmlns="http://tempuri.org/">{"code":0,"message":"ok","content":"206"}</string> -
-
amigo le añadí este código:
XmlDocument doc = JsonConvert.DeserializeXmlNode(response, "rootNodeName");
return (doc.InnerXml);---------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Runtime.Serialization.Json;
using System.Web.ClientServices;
using System.Data;
using System.Net;
using System.Runtime.Serialization;
using System.IO;
using System.Text;
using WebServiceElockers.CapaEntidades;
using Newtonsoft.Json;
using System.Collections.Specialized;
using System.Xml;
using System.Xml.Serialization;namespace WebServiceElockers
{[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]public class WebServiceBoxeway : System.Web.Services.WebService
{
[WebMethod]
public string ReservaLocker(int CodigoEmpresa,int CodLocker,string CodSize,string DestinatarioNombre,string Referencia,string DestinatarioEmail,string DestinatarioCelular,int TiempoReserva)
{
var cli = new WebClient();
cli.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
cli.Headers[HttpRequestHeader.Accept] = "application/json";string token = "xxxxxxx";
string data = "token="+token+"&id_elocker="+CodLocker+"&id_locker_size="+CodSize+"&receipt_name="+DestinatarioNombre+"&email="+DestinatarioEmail+"&phone="+DestinatarioCelular+"&duration_in_minutes="+TiempoReserva;
string response = cli.UploadString("http://sandbox-api.boxeway.com/api/pickup//booking", "POST", data);
XmlDocument doc = JsonConvert.DeserializeXmlNode(response, "rootNodeName");
return (doc.InnerXml);}
}
}
//----------------------------------------------------------------------------------------------------------------------------------
me sale esto:
This XML file does not appear to have any style information associated with it. The document tree is shown below.<string xmlns="http://tempuri.org/"><rootNodeName><code>0</code><message>ok</message><content>209</content></rootNodeName></string>Yo quiero que me muestre así:
<Rpta>ok</Rpta>
<Cod.Operacion>209</Cod.Operacion>
Gracias.
-
por favor ayuden quiero que me muestre así:
<Rpta>ok</Rpta>
<Cod.Operacion>209</Cod.Operacion>
pero me sale así:
This XML file does not appear to have any style information associated with it. The document tree is shown below.<string xmlns="http://tempuri.org/"><rootNodeName><code>0</code><message>ok</message><content>209</content></rootNodeName></string> -
Buenas tardes estoy trabjando con Webservice y al ejecutar. más abajo les muestro mi codigo, por favor ayuden.
me sale esto:
This XML file does not appear to have any style information associated with it. The document tree is shown below.<string xmlns="http://tempuri.org/"><rootNodeName><code>0</code><message>ok</message><content>209</content></rootNodeName></string>Yo quiero que me muestre así:
<Rpta>ok</Rpta>
<Cod.Operacion>209</Cod.Operacion>
Gracias.
//-----------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Runtime.Serialization.Json;
using System.Web.ClientServices;
using System.Data;
using System.Net;
using System.Runtime.Serialization;
using System.IO;
using System.Text;
using WebServiceElockers.CapaEntidades;
using Newtonsoft.Json;
using System.Collections.Specialized;
using System.Xml;
using System.Xml.Serialization;namespace WebServiceElockers
{[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]public class WebServiceBoxeway : System.Web.Services.WebService
{
[WebMethod]
public string ReservaLocker(int CodigoEmpresa,int CodLocker,string CodSize,string DestinatarioNombre,string Referencia,string DestinatarioEmail,string DestinatarioCelular,int TiempoReserva)
{
var cli = new WebClient();
cli.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
cli.Headers[HttpRequestHeader.Accept] = "application/json";string token = "xxxxxxx";
string data = "token="+token+"&id_elocker="+CodLocker+"&id_locker_size="+CodSize+"&receipt_name="+DestinatarioNombre+"&email="+DestinatarioEmail+"&phone="+DestinatarioCelular+"&duration_in_minutes="+TiempoReserva;
string response = cli.UploadString("http://sandbox-api.boxeway.com/api/pickup//booking", "POST", data);
XmlDocument doc = JsonConvert.DeserializeXmlNode(response, "rootNodeName");
return (doc.InnerXml);}
}
}
//----------------------------------------------------------------------------------------------------------------------------------
- Combinado Sergio ParraModerator martes, 12 de septiembre de 2017 5:31 Duplicada
-
hola
pero estas retornando como respuesta el json que obtienes de la invocacion por WebClient, si quieres otra respues deberas transformar los datos
deberias tomar la respuesta del json y convertir a otro objeto diferente
public static string SerializeObject<T>(T toSerialize) { XmlSerializer xmlSerializer = new XmlSerializer(toSerialize.GetType()); using(StringWriter textWriter = new StringWriter()) { xmlSerializer.Serialize(textWriter, toSerialize); return textWriter.ToString(); } }
entonces defines la clase
public class Reserva{ public string Rpta {get;set;} public string CodOperacion{get;set;} }
para luego poder convertir
string response = cli.UploadString("http://sandbox-api.boxeway.com/api/pickup//booking", "POST", data); XmlDocument doc = JsonConvert.DeserializeXmlNode(response, "rootNodeName"); var result = new Reserva(){ Rpta = doc.Node("xx").Value, CodOperacion = doc.Node("xx").Value } return SerializeObject<Reserva>(result);
pero retornas la serializacion de tu clase y no la que obtuviste de la llamada
saludos
Leandro Tuttini
Blog
MVP Profile
Buenos Aires
Argentina- Propuesto como respuesta Jorge TurradoMVP martes, 12 de septiembre de 2017 7:34
- Marcado como respuesta Moderador MModerator lunes, 18 de septiembre de 2017 16:11
-
Mostrar un XML, darle un formato desde un WebSservice C#.
Leandro Tuttini
Blog
MVP Profile
Buenos Aires
Argentina