簡體   English   中英

從SOAP消息中提取SOAP主體

[英]Extract SOAP body from a SOAP message

我想從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); 

錯誤:

指數超出范圍。 必須是非負數且小於集合的大小。

如果有人知道如何解決這個問題?

對於這樣的請求:

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

Linq2Xml更易於使用。

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元素:

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);
}

您可以使用GetElementsByTagName來提取soap請求的正文。

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);
    }
}

簡單的解決方案,如果身體有多個孩子,你只需要刪除信封:

    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.LoadXml(placeYourXmlHere);

    if (xmlDoc.DocumentElement.Name == "soapenv_Envelope")
    {
        string tempXmlString = xmlDoc.DocumentElement.InnerXml;
        xmlDoc.LoadXml(tempXmlString);
    }

如果要刪除Envelope和body,其中body僅包含一個子節點:

    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.LoadXml(placeYourXmlHere);

    while (xmlDoc.DocumentElement.Name == "soapenv_Envelope" || xmlDoc.DocumentElement.Name == "soapenv_Body")
    {
        string tempXmlString = xmlDoc.DocumentElement.InnerXml;
        xmlDoc.LoadXml(tempXmlString);
    }

現在xml被縮減為Body的內容

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM