从c#中的SOAP响应中检索内部元素

时间:2018-06-19 05:59:20

标签: c# soap soap-client

我需要SOAP POST请求响应中的特定元素。我的实际请求是使用HTTP客户端完成的,这非常有效。我一直试图从这个回应中拉出一个内在元素,以便在我的应用程序中使用,但我还没有找到方法。这是我第一次使用SOAP请求,所以任何帮助都将受到赞赏。

以下是我对PostMan的回复:我需要检索"客户名称"

的值
<?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>
        <Query_With_StringResponse xmlns="http://www.syspro.com/ns/query/">
            <Query_With_StringResult>&lt;?xml version="1.0" encoding="Windows-1252"?&gt;
&lt;ARListOfCustomers Language='05' Language2='EN' CssStyle='' DecFormat='1' DateFormat='01' Role='01' Version='7.0.005' OperatorPrimaryRole='   '   &gt;
&lt;QueryOptions&gt;
&lt;ReportSequence&gt;CU&lt;/ReportSequence&gt;
&lt;PriceCode/&gt;
&lt;PriceProductmatrix&gt;N&lt;/PriceProductmatrix&gt;
&lt;ExtraFields&gt;N&lt;/ExtraFields&gt;
&lt;InterestExemptionStatusSelection&gt;A&lt;/InterestExemptionStatusSelection&gt;
&lt;TaxExemptionSelection&gt;A&lt;/TaxExemptionSelection&gt;
&lt;CustomerSelectionFilterType&gt;A&lt;/CustomerSelectionFilterType&gt;
&lt;CustomerSelectionFilterValue/&gt;
&lt;CustomerClassSelectionFilterType&gt;A&lt;/CustomerClassSelectionFilterType&gt;
&lt;CustomerClassSelectionFilterValue/&gt;
&lt;GeographicAreaSelectionFilterType&gt;A&lt;/GeographicAreaSelectionFilterType&gt;
&lt;GeographicAreaSelectionFilterValue/&gt;
&lt;BranchSelectionFilterType&gt;A&lt;/BranchSelectionFilterType&gt;
&lt;BranchSelectionFilterValue/&gt;
&lt;SalespersonSelectionFilterType&gt;A&lt;/SalespersonSelectionFilterType&gt;
&lt;SalespersonSelectionFilterValue/&gt;
&lt;LineDiscountCodeSelectionFilterType&gt;A&lt;/LineDiscountCodeSelectionFilterType&gt;
&lt;LineDiscountCodeSelectionFilterValue/&gt;
&lt;TermsSelectionFilterType&gt;A&lt;/TermsSelectionFilterType&gt;
&lt;TermsSelectionFilterValue/&gt;
&lt;ProductCategorySelectionFilterType&gt;A&lt;/ProductCategorySelectionFilterType&gt;
&lt;ProductCategorySelectionFilterValue/&gt;
&lt;InvoiceDiscountCodeSelectionFilterType&gt;A&lt;/InvoiceDiscountCodeSelectionFilterType&gt;
&lt;InvoiceDiscountCodeSelectionFilterValue/&gt;
&lt;CurrencySelectionFilterType&gt;A&lt;/CurrencySelectionFilterType&gt;
&lt;CurrencySelectionFilterValue/&gt;
&lt;CreditLimitSelectionFilterType&gt;A&lt;/CreditLimitSelectionFilterType&gt;
&lt;CreditLimitSelectionFilterValue/&gt;
&lt;/QueryOptions&gt;
&lt;Customer&gt;
&lt;CustomerListHeader&gt;
&lt;Customer&gt;TSAR&lt;/Customer&gt;
&lt;CustomerName&gt;TSAR BUSINESS SOLUTION&lt;/CustomerName&gt;
&lt;CustomerShortName&gt;TSAR BUSINESS SOLUTI&lt;/CustomerShortName&gt;
&lt;Branch&gt;TSAR&lt;/Branch&gt;
&lt;BranchDescription&gt;HEAD OFFICE  TSAR&lt;/BranchDescription&gt;
&lt;Geography&gt;031&lt;/Geography&gt;
&lt;GeographyDescription&gt;DURBAN&lt;/GeographyDescription&gt;
&lt;Class/&gt;
&lt;ClassDescription&gt;** Not on file **&lt;/ClassDescription&gt;
&lt;BalanceType&gt;Op-item&lt;/BalanceType&gt;
&lt;Sales&gt;IVAN&lt;/Sales&gt;
&lt;CreditLimit&gt;           0&lt;/CreditLimit&gt;
&lt;Currency&gt;R&lt;/Currency&gt;
&lt;CurrencyDescription&gt;Rand&lt;/CurrencyDescription&gt;
&lt;Telephone/&gt;
&lt;InvoiceTermsCode&gt;CO&lt;/InvoiceTermsCode&gt;
&lt;TermsCodeDescription&gt;CASH ON DELIVERY&lt;/TermsCodeDescription&gt;
&lt;/CustomerListHeader&gt;
&lt;CustomerListDetails&gt;
&lt;Contact/&gt;
&lt;TaxNo&gt;Tax No:&lt;/TaxNo&gt;
&lt;SpecialInstructions/&gt;
&lt;SoldToAddress1/&gt;
&lt;SoldToAddress2/&gt;
&lt;SoldToAddress3/&gt;
&lt;SoldToAddress3Loc/&gt;
&lt;SoldToAddress4/&gt;
&lt;SoldToAddress5/&gt;
&lt;SoldToAddress6/&gt;
&lt;SoldToGpsLat&gt;  0.000000&lt;/SoldToGpsLat&gt;
&lt;SoldToGpsLong&gt;   0.000000&lt;/SoldToGpsLong&gt;
&lt;ShipToAddress1&gt;STRAUSS DALY&lt;/ShipToAddress1&gt;
&lt;ShipToAddress2&gt;41 RICHFONT CRICLE&lt;/ShipToAddress2&gt;
&lt;ShipToAddress3&gt;DURBAN&lt;/ShipToAddress3&gt;
&lt;ShipToAddress3Loc/&gt;
&lt;ShipToAddress4&gt;KZB&lt;/ShipToAddress4&gt;
&lt;ShipToAddress5&gt;SOUTH AFRICA&lt;/ShipToAddress5&gt;
&lt;ShipToAddress6&gt;4000&lt;/ShipToAddress6&gt;
&lt;ShipToGpsLat&gt;  0.000000&lt;/ShipToGpsLat&gt;
&lt;ShipToGpsLong&gt;   0.000000&lt;/ShipToGpsLong&gt;
&lt;GSTNumber/&gt;
&lt;LineDiscCode/&gt;
&lt;InvDiscCode/&gt;
&lt;DefaultPriceCode/&gt;
&lt;CompanyTaxNumber/&gt;
&lt;ExemptFinChg&gt;No finance charges&lt;/ExemptFinChg&gt;
&lt;/CustomerListDetails&gt;
&lt;/Customer&gt;
&lt;ReportSummary&gt;
&lt;NoOfCustomersListed&gt;    1&lt;/NoOfCustomersListed&gt;
&lt;/ReportSummary&gt;
&lt;/ARListOfCustomers&gt;
 </Query_With_StringResult>
        </Query_With_StringResponse>
    </soap:Body>
</soap:Envelope>

这是我的申请中的请求:c#

 public async Task<string> CreateSoapEnvelop()
        {
            string soapString = @"<?xml version=""1.0"" encoding=""utf-8""?>
            <soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">
                <soap:Body>
                 <Query_With_String xmlns=""http://www.syspro.com/ns/query/"">
                <UserId>" + Settings.GUID + @"</UserId>
                <BusinessObject></BusinessObject>
                <XMLIn></XMLIn>
                </Query_With_String>
                </soap:Body>
            </soap:Envelope>";

            HttpResponseMessage response = await PostXmlRequest("http://sysprowebservices/query.asmx", soapString);
            var soapResponse = await response.Content.ReadAsStringAsync();

            try
            {
                var soap = XDocument.Parse(soapResponse);
                XNamespace NS = "http://www.syspro.com/ns/query/";
                var result = soap.Descendants(NS + "Query_With_StringResponse").First().Element(NS + "Query_With_StringResult").Value;


            }
            catch(Exception ex)
            {
                string message = ex.Message;
            }


            return soapResponse;
        }

        public static async Task<HttpResponseMessage> PostXmlRequest(string baseUrl, string xmlString)
        {
            using (var httpClient = new HttpClient())
            {
                var httpContent = new StringContent(xmlString, Encoding.UTF8, "text/xml");
                httpContent.Headers.Add("SOAPAction", "http://www.syspro.com/ns/query/Query_With_String");

                return await httpClient.PostAsync(baseUrl, httpContent);
            }
        }

0 个答案:

没有答案
相关问题