开发者

Using C# to parse a SOAP Response

I am trying to get the values for faultcode, faultstring, and OrderNumber from the 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 am able to get the values for both faultcode and faultstring but not the Order开发者_JAVA百科Number. I am getting "Enumeration yielded no results." Can anyone help? Thanks.


Yes, you're ignoring the XML namespace when selecting:

<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 <Order> tag is inside the <m:SaveOrder> tag which uses the XML namespace prefixed by the m: prefix.

Also, you're trying to select the "detail" and then you skip to the "Order" node directly (using .Elements()) - you missed the <m:SaveOrder> node in between.

You need to take that into account when selecting:

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;

Does that give you a result??


A clean solution could be using System.Xml.XPath. Here is where I found the example

This is how to use it in this case:

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

XmlNamespaceManager xmlnsManager = new XmlNamespaceManager(xmlDoc.NameTable);

xmlnsManager.AddNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/");
xmlnsManager.AddNamespace("m", "http://www.test.com/software/schema/");

var orderNode = xmlDoc.SelectSingleNode("//m:Order", xmlnsManager);
var value = orderNode.Attributes["OrderNumber"].Value;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜