c# - 从 SOAP 消息中提取 SOAP 主体

标签 c# soap xml-parsing

我想从 SOAP 消息中提取 SOAP 主体,我在 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);
}

这里有一个错误:

int iSoapBodyElementStartEnd = SoapMessage.IndexOf(">", iSoapBodyElementStartFrom); 

错误:

Index was out of range. Must be non-negative and less than the size of the collection.

谁知道怎么解决?

最佳答案

对于这样的请求:

String request = @"<?xml version=""1.0"" encoding=""UTF-8""?>
    <soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""
    xmlns:soapenc=""http://schemas.xmlsoap.org/soap/encoding/""
    xmlns:xsd=""http://www.w3.org/2001/XMLSchema""
    xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">
    <soap:Body>
    <ResponseData xmlns=""urn:Custom"">some data</ResponseData>
    </soap:Body>
    </soap:Envelope>";

以下代码完成了对数据进行解包并仅获取 <ReponseData> 的工作。 xml内容:

XDocument xDoc = XDocument.Load(new StringReader(request));

var unwrappedResponse = xDoc.Descendants((XNamespace)"http://schemas.xmlsoap.org/soap/envelope/" + "Body")
    .First()
    .FirstNode

关于c# - 从 SOAP 消息中提取 SOAP 主体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10294544/

相关文章:

c# - 枚举类型 : variable 'x' of type 'xxx' referenced from scope '' , 的起订量子属性,但未定义

php - 使用 Zend Framework 通过 SSL 进行 SOAP 调用

java - 序列化整数[]

ios - 如何解析网站文本并在 View 中显示

C# - 无需强制转换即可将对象转换为 int

c# - 为什么我需要成为管理员才能读取 .NET CLR 内存性能计数器

C# 运算符重载 ==

java - 尝试使用 Web 服务时出现 SOAPFAULTEXCEPTION

python - 编写/解析 Apple Motion 5 .motn 文件

r - R中的PCDATA无效字符