Reputation: 35
I am new to C# Soap Web Service. I am able to make a Soap Request. But not sure how to parse the soap response below to get OrderId for example.
<?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>
Any help will be greatly appreciated.
With the below sample code I was able to parse simple 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);
}
But the moment I include <OrderCreateByChannelResponse xmlns=""http://retailexpress.com.au/"">
in the XML string doesn't work.
Upvotes: 2
Views: 1713
Reputation: 1545
Try like this
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;
}
Upvotes: 2