Principales respuestas
Como leer un webservice que regresa un XML?

Pregunta
-
tengo el siguiente web service que me regresa un XML, quisiera leerlo y el resultado mostrarlo en grid.
[WebMethod]
public XmlDocument ListarOrdenTrabajo()
{
DataTable DT = Fn.Ejecutar("Guido_ListarOrdenTrabajo");
XmlDocument xml = new XmlDocument();
if (DT.Rows.Count > 0)
{
DataSet ds = new DataSet("xml");
ds.Tables.Add(DT);
if (File.Exists(HttpContext.Current.Server.MapPath("xmlListarOrdenTrabajo.xml")))
{
File.Delete(HttpContext.Current.Server.MapPath("xmlListarOrdenTrabajo.xml"));
}
XmlTextWriter stream = new XmlTextWriter(HttpContext.Current.Server.MapPath("xmlListarOrdenTrabajo.xml"), Encoding.UTF8);
stream.WriteStartDocument();
ds.WriteXml(stream);
stream.Close();
string fic = HttpContext.Current.Server.MapPath("xmlListarOrdenTrabajo.xml");
System.IO.StreamReader sr = new System.IO.StreamReader(fic);
string xmlstr = sr.ReadToEnd();
xml.LoadXml(xmlstr);
sr.Close();
}
return xml;
}
Guido.
Respuestas
-
en esta ocasión me quiero contestar, cuando se tiene un webservice que regresa un xmldocument.
ester seria el webservice de ejemplo
[WebMethod]
public XmlDocument ReturnXml()
{
string xmlString = "<book><name>good</name><author>Davie</author></book>"; XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlString);
return doc;
}
de esta forma se llamaría y consumiría
DataSet ds = new DataSet();
swTaller.wsTallerSoapClient svc = new swTaller.wsTallerSoapClient();
string s = svc. ReturnXml().ToString();
System.IO.MemoryStream m = new System.IO.MemoryStream(System.Text.Encoding.ASCII.GetBytes(s));
ds.ReadXml(m);
gridControl1.DataSource = ds.Tables[0];saludos
Guido.
- Marcado como respuesta denisenrique martes, 17 de marzo de 2015 15:18
- Editado denisenrique martes, 17 de marzo de 2015 16:32
Todas las respuestas
-
hola
el servicio web deberias retornar el xml como un string, hasta donde se XmlDocument no es serializable por lo que tendras problemas para referenciar
public string ListarOrdenTrabajo(){
//resto codigo
return xml.ToString();
}
saludos
Leandro Tuttini
Blog
MVP Profile
Buenos Aires
Argentina -
lo leo de la siguiente manera y me da error
este el codigo
swTaller.wsTallerSoapClient svc = new swTaller.wsTallerSoapClient();
string strXml = svc.ListarOrdenTrabajo();
// Read the XML data into the local DataSet.
DataSet ds =new DataSet();
System.IO.StringReader sr = new System.IO.StringReader(strXml);
ds.ReadXml(sr);
ds.AcceptChanges();
gridControl1.DataSource=ds.Tables[0].DefaultView;Guido.
-
en esta ocasión me quiero contestar, cuando se tiene un webservice que regresa un xmldocument.
ester seria el webservice de ejemplo
[WebMethod]
public XmlDocument ReturnXml()
{
string xmlString = "<book><name>good</name><author>Davie</author></book>"; XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlString);
return doc;
}
de esta forma se llamaría y consumiría
DataSet ds = new DataSet();
swTaller.wsTallerSoapClient svc = new swTaller.wsTallerSoapClient();
string s = svc. ReturnXml().ToString();
System.IO.MemoryStream m = new System.IO.MemoryStream(System.Text.Encoding.ASCII.GetBytes(s));
ds.ReadXml(m);
gridControl1.DataSource = ds.Tables[0];saludos
Guido.
- Marcado como respuesta denisenrique martes, 17 de marzo de 2015 15:18
- Editado denisenrique martes, 17 de marzo de 2015 16:32