Obtener los datos XML de un Documento SOAP

Enero 09, 2009 :: Posted by - Emilio Torrens :: Category - , ,

Mas de mi servicio "impersonal" …

Resulta que ahora también recibe documentos SOAP, así que tengo que extraer los datos del mensaje SOAP con este código:

private XmlDocument GetSOAPBody(XmlDocument xmlSoap)
    {

              const string SOAP_NAMESPACE = "http://schemas.xmlsoap.org/soap/envelope/";
              XmlNodeList nodes = xmlSoap.GetElementsByTagName("Body", SOAP_NAMESPACE);

          if (nodes.Count == 0) return xmlSoap;

          XmlElement body = (XmlElement)nodes.Item(0);

          nodes = body.ChildNodes;
          for (int i = 0; i < nodes.Count; i++)
          {
              XmlNode node = nodes.Item(i);
              if (node is XmlElement)
              {
                  xmlSoap = new XmlDocument();
                  xmlSoap.LoadXml(node.OuterXml);
                  IsSOAP = true;
                  return xmlSoap;
              }
          }

          return xmlSoap;
      }  

Funciona bastante bien, ahora estoy con el de generar las respuestas de OK y de Error, ya lo publicare :)

Cliente SOAP para servicios PHP o Java AXIS

Abril 14, 2008 :: Posted by - Emilio Torrens :: Category - , , ,

Aquí dejo una clase para consumir servicios web vía SOAP en los que no tengamos el WSDL o que el Wizard nos de problemas:

using System;
using System.Text;
using System.IO;
using System.Net;
using System.Xml;

namespace Test
    {
    class SOAPClient
        {

        public static XmlDocument SendMsg(XmlDocument xmlRQ)
            {
                byte[] byte1;
                Stream myStream;
                StreamReader readStream;

                HttpWebRequest wr =
                    (HttpWebRequest)WebRequest.
                    Create("http://laurl.com/ws.php");

                byte1 =
                Encoding.GetEncoding(1252).GetBytes(xmlRQ.OuterXml);
                wr.Method = "POST";
                wr.Headers.Add("SOAPAction", "");
                wr.ContentType = "text/xml; charset=\"utf-8\"";
                wr.Accept = "text/xml";

                myStream = wr.GetRequestStream();
                myStream.Write(byte1, 0, byte1.Length);
                myStream.Close();

                myStream = wr.GetResponse().GetResponseStream();
                readStream = new StreamReader(myStream);
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(readStream);
                myStream.Close();

                return getSOAPPayload(xmlDoc, "body");
            }

          private static XmlDocument getSOAPPayload(XmlDocument rs,
                                                    string Tag)
            {

                XmlNode xmlNode =
                    rs.GetElementsByTagName(Tag)[0].FirstChild;
                XmlDocument xmlResult = new XmlDocument();
                xmlResult.LoadXml(xmlNode.Value);
                return xmlResult;
            }

        }
    }