C# - 解析 Soap 响应以提取数据

C# - Parse Soap response to extract data

我是 C# Soap Web 服务的新手。我可以提出 Soap 请求。但不确定如何解析下面的 soap 响应以获取 OrderId。

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <OrderCreateByChannelResponse
            xmlns="http://retailexpress.com.au/">
            <OrderCreateByChannelResult>
                <Response
                    xmlns="">
                    <OrderCreate>
                        <Customer>
                            <Result>Success</Result>
                            <ExternalCustomerId>170</ExternalCustomerId>
                            <CustomerId>300941</CustomerId>
                        </Customer>
                        <Order>
                            <Result>Success</Result>
                            <ExternalOrderId>INT_3673_ccv_20190926132240</ExternalOrderId>
                            <OrderId>19-00033544</OrderId>
                            <ChannelID>1</ChannelID>
                        </Order>
                        <OrderItems>
                            <OrderItem>
                                <Result>Success</Result>
                                <ExternalOrderItemId></ExternalOrderItemId>
                                <ProductId>126659</ProductId>
                                <OrderId>19-00033544</OrderId>
                            </OrderItem>
                        </OrderItems>
                        <OrderPayments>
                            <OrderPayment>
                                <Result>Success</Result>
                                <Amount>23.45</Amount>
                                <MethodId>38</MethodId>
                                <OrderId>19-00033544</OrderId>
                            </OrderPayment>
                        </OrderPayments>
                    </OrderCreate>
                </Response>
            </OrderCreateByChannelResult>
        </OrderCreateByChannelResponse>
    </soap:Body>
</soap:Envelope>

任何帮助将不胜感激。

通过下面的示例代码,我能够解析简单的 XML

string  testXML1 = @"<OrderCreateByChannelResult><Response xmlns=""""><OrderCreate><Customer><Result>Success</Result><ExternalCustomerId>170</ExternalCustomerId><CustomerId>300940</CustomerId></Customer></OrderCreate></Response></OrderCreateByChannelResult>";
XmlDocument xdoc = new XmlDocument();//xml doc used for xml parsing
xdoc.LoadXml(testXML1);
XmlNodeList result = xdoc.SelectNodes("/OrderCreateByChannelResult/Response/OrderCreate/Customer");
foreach (XmlNode xn in result)
{
  string ExternalCustomerId = xn["ExternalCustomerId"].InnerText;
  Response.Write("ExternalCustomerId = " + ExternalCustomerId);
}

但是我在 XML 字符串中包含 <OrderCreateByChannelResponse xmlns=""http://retailexpress.com.au/""> 的那一刻不起作用。

这样试试

XDocument doc = XDocument.Parse(testXML1);
XNamespace ns = doc.Root.GetDefaultNamespace();
string ExternalCustomerId="";    
string CustomerId=""; 
foreach (XElement element in doc.Descendants(ns + "ExternalCustomerId"))
 {
   ExternalCustomerId = element.Value;
 }
 foreach (XElement element in doc.Descendants(ns + "CustomerId"))
 {
   CustomerId = element.Value;
 }