Quiero extraer cuerpo SOAP de un mensaje SOAP, tengo algunos datos en cuerpo SOAP que tengo que analizar en la base de la fecha, por lo que este es el código:Extracto de cuerpo SOAP de un mensaje SOAP
public string Load_XML(string SoapMessage)
{
//check soap message
if (SoapMessage == null || SoapMessage.Length <= 0)
throw new Exception("Soap message not valid");
//declare some local variable
int iSoapBodyStartIndex = 0;
int iSoapBodyEndIndex = 0;
//load the Soap Message
//Učitaj string XML-a i pretvori ga u XML
XmlDocument doc = new XmlDocument();
try
{
doc.Load(SoapMessage);
}
catch (XmlException ex)
{
WriteErrors.WriteToLogFile("WS.LOAD_DOK_LoadXML", ex.ToString());
throw ex;
}
//search for the "http://schemas.xmlsoap.org/soap/envelope/" URI prefix
string prefix = string.Empty;
for (int i = 0; i < doc.ChildNodes.Count; i++)
{
System.Xml.XmlNode soapNode = doc.ChildNodes[i];
prefix = soapNode.GetPrefixOfNamespace("http://schemas.xmlsoap.org /soap/envelope/");
if (prefix != null && prefix.Length > 0)
break;
}
//prefix not founded.
if (prefix == null || prefix.Length <= 0)
throw new Exception("Can't found the soap envelope prefix");
//find soap body start index
int iSoapBodyElementStartFrom = SoapMessage.IndexOf("<" + prefix + ":Body");
int iSoapBodyElementStartEnd = SoapMessage.IndexOf(">", iSoapBodyElementStartFrom); -> HERE I HAVE AN ERROR!!!!
iSoapBodyStartIndex = iSoapBodyElementStartEnd + 1;
//find soap body end index
iSoapBodyEndIndex = SoapMessage.IndexOf("</" + prefix + ":Body>") - 1;
//get soap body (xml data)
return SoapMessage.Substring(iSoapBodyStartIndex, iSoapBodyEndIndex - iSoapBodyStartIndex + 1);
}
tengo un error aquí:
int iSoapBodyElementStartEnd = SoapMessage.IndexOf(">", iSoapBodyElementStartFrom);
El error:
Index was out of range. Must be non-negative and less than the size of the collection.
Si alguien sabe cómo solucionar esto?
¿Es definitivamente no negativo? Supongo que es -1 porque el bloque de inicio no coincidía en la cadena. ¿Qué hay en la cadena? – Rup
También verificaría si 'prefix' se lee correctamente. Su espacio de nombres SOAP no debe tener espacios en el medio - ¿elimina esa ayuda? También puede ser mejor utilizar un analizador XML adecuado aquí en lugar de la coincidencia de subcadenas. – Rup
¿Qué quiere decir con un analizador XML correcto? No te estoy siguiendo? Esto es lo que quiero leer Xml version = "1.0" encoding = "UTF-8"?> soap: envelope> –
CrBruno