Using C # to parse a SOAP response

I am trying to get values ​​for error code, faultstring and OrderNumber from SOAP below

<SOAP:Envelope xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP:Body>
        <faultcode>1234</faultcode> 
        <faultstring>SaveOrder:SetrsOrderMain:Cannot change OrderDate if GLPeriod is closed, new OrderDate is 3/2/2010:Ln:1053</faultstring>               
        <detail>
           <SOAP:Envelope xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/">
           <SOAP:Body UserGUID="test">
           <m:SaveOrder xmlns:m="http://www.test.com/software/schema/" UserGUID="test">
                <Order OrderNumber="1234-1234-123" Caller="" OrderStatus="A" xmlns="http://www.test.com/software/schema/">

Here is my code in C #

XDocument doc = XDocument.Load(HttpContext.Current.Server.MapPath("XMLexample.xml"));

var errorDetail = new EcourierErrorDetail
{
   FaultCode = from fc in doc.Descendants("faultcode")
               select fc.Value,
   FaultString = from fs in c.Descendants("faultstring")
                 select fs.Value,
   OrderNumber = from o in    
                 doc.Descendants("detail").Elements("Order").Attributes("OrderNumber")
                 select o.Value
};
return errorDetail;

I can get values ​​for both the error code and the faultstring string, but not for OrderNumber. I get: "The listing did not produce any results." Can anyone help? Thank.

+3
source share
1 answer

Yes, you ignore the XML namespace when choosing:

<SOAP:Envelope xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/">
   .....
   <m:SaveOrder xmlns:m="http://www.test.com/software/schema/" UserGUID="test">
     <Order OrderNumber="1234-1234-123" Caller="" OrderStatus="A" xmlns="http://www.test.com/software/schema/">

The tag <Order>is inside the tag <m:SaveOrder>that uses the XML namespace, the prefix prefix m:.

, "", "" node ( .Elements()) - <m:SaveOrder> node .

:

XDocument doc = XDocument.Load(HttpContext.Current.Server.MapPath("XMLexample.xml"));

XNamespace xmlns = "http://www.test.com/software/schema/";

var orderNode = doc.Descendants(xmlns + "SaveOrder").Elements(xmlns + "Order");

var value = from o in orderNode.Attributes("OrderNumber")
            select o.Value;

?

+6

Source: https://habr.com/ru/post/1746423/


All Articles