首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从SOAP消息中提取SOAP主体

从SOAP消息中提取SOAP主体
EN

Stack Overflow用户
提问于 2012-04-24 08:44:04
回答 4查看 40.2K关注 0票数 16

我想从SOAP消息中提取SOAP主体,我必须在数据基中解析SOAP主体中的一些数据,所以这是代码:

代码语言:javascript
复制
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);
}

我这里有个错误:

代码语言:javascript
复制
int iSoapBodyElementStartEnd = SoapMessage.IndexOf(">", iSoapBodyElementStartFrom); 

错误:

指数超出范围。必须是非负的,并且小于集合的大小。

如果有人知道怎么解决这个问题?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2013-04-02 15:32:33

对于这样的请求:

代码语言:javascript
复制
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内容的工作:

代码语言:javascript
复制
XDocument xDoc = XDocument.Load(new StringReader(request));

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

Stack Overflow用户

发布于 2012-04-24 13:49:28

Linq2Xml更易于使用。

代码语言:javascript
复制
string xml = @"<?xml version=""1.0"" encoding=""UTF-8"" ?>
    <soap:envelope xmlns:xsd=""w3.org/2001/XMLSchema"" xmlns:xsi=""w3.org/2001/XMLSchema-instance"" xmlns:soap=""schemas.xmlsoap.org/soap/envelope/"">; 
    <soap:body> 
    <order> <id>1234</id>  </order> 
    </soap:body> 
    </soap:envelope>";

XDocument xDoc = XDocument.Load(new StringReader(xml));
var id =  xDoc.Descendants("id").First().Value;

-编辑--

body中循环元素

代码语言:javascript
复制
XDocument xDoc = XDocument.Load(new StringReader(xml));
XNamespace soap = XNamespace.Get("schemas.xmlsoap.org/soap/envelope/");

var items = xDoc.Descendants(soap+"body").Elements();
foreach (var item in items)
{
    Console.WriteLine(item.Name.LocalName);
}
票数 12
EN

Stack Overflow用户

发布于 2016-05-01 16:46:00

您可以利用GetElementsByTagName提取soap请求的主体。

代码语言:javascript
复制
private static T DeserializeInnerSoapObject<T>(string soapResponse)
{
    XmlDocument xmlDocument = new XmlDocument();
    xmlDocument.LoadXml(soapResponse);

    var soapBody = xmlDocument.GetElementsByTagName("soap:Body")[0];
    string innerObject = soapBody.InnerXml;

    XmlSerializer deserializer = new XmlSerializer(typeof(T));

    using (StringReader reader = new StringReader(innerObject))
    {
        return (T)deserializer.Deserialize(reader);
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10294544

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档