【问题标题】:How to filter SOAP results client side in C#?如何在 C# 中过滤 SOAP 结果客户端?
【发布时间】:2019-12-24 10:47:52
【问题描述】:

我向电子商务 API 发出 SOAP 请求,以了解在给定时间范围内为指定产品订购了多少订单。

我的方法需要一个产品 ID 和天数来创建一个时间范围。然后它构造一个 SOAP 请求并返回销售产品的数量。

public static async Task<int?> GetOrdersFromApi(int providedId, int days) {
            var dateFrom = DateTime.Now.AddDays(days * -1).ToString("yyyy-MM-dd") + " 00:00:00";
            var dateTo = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            float orderedCount = 0;
            int orderedCountToPass = 0;
            int i = 0;

            var binding = new BasicHttpBinding {
                MaxReceivedMessageSize = 40000000
            };
            var address = new EndpointAddress("http://example.com/api/get/");
            using (var client = new ApiOrdersPortTypeClient(binding, address)) {
                try {
                    while (true) {
                        // request parameters
                        var request = new ApiOrdersGetAsync.requestType {
                            authenticate = new ApiOrdersGetAsync.authenticateType {
                                userLogin = "Username",
                                authenticateKey = "Key",
                            }
                        };
                        request.@params = new ApiOrdersGetAsync.paramsType {
                            ordersStatuses = new string[] { "finished" },
                            products = new productType[1],
                        };
                        request.@params.products[0] = new productType {
                            productIdSpecified = true,
                            productId = providedId,
                        };
                        request.@params.ordersRange = new ordersRangeType {
                            ordersDateRange = new ordersDateRangeType()
                        };
                        request.@params.ordersRange.ordersDateRange.ordersDateTypeSpecified = true;
                        request.@params.ordersRange.ordersDateRange.ordersDateType = ordersDateTypeType.dispatch;
                        request.@params.ordersRange.ordersDateRange.ordersDateBegin = dateFrom;
                        request.@params.ordersRange.ordersDateRange.ordersDateEnd = dateTo;
                        request.@params.resultsPageSpecified = true;
                        request.@params.resultsPage = i;

                        // processing the result
                        var results = await client.getAsync(request);

                        foreach (var result in results.Results) {
                            int productsResultsPage = 0;
                            foreach (var product in result.orderDetails.productsResults) {
                                try {
                                    if (result.orderDetails.productsResults[productsResultsPage ].productId == providedId) {
                                        orderedCount += result.orderDetails.productsResults[y].productQuantity;
                                        productsResultsPage++;
                                    }
                                } catch (IndexOutOfRangeException ex) {
                                    // error is thrown to escape loop - sloppy, I know
                                    Console.WriteLine(ex); 
                                };
                            }
                        };
                        orderedCountToPass = (int)orderedCount;
                        orderedCount = 0;
                        i++;
                    };
                } catch (NullReferenceException) { 
                    // do nothing, we just want to exit while loop when i is higher than page count
                }
                return orderedCountToPass;
            };
        }

结果通常应该是数百,但无论产品卖得多好,它都会返回从 0 到 4 的东西。

这是一个示例响应:

例如,我只对productId == 479 感兴趣,但还订购了我不感兴趣的其他产品 - 我需要过滤它们。

我尝试过滤结果的方式有问题。我该如何正确地做到这一点?我确定请求是正确的,并且响应确实包含所有可能的订单。

【问题讨论】:

    标签: c# soap


    【解决方案1】:

    您正在获取 xml 结果,因此请使用 Net Xml 库:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using System.Xml.Linq;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            const string URL = @"Enter URL Here";
            static void Main(string[] args)
            {
    
                XDocument doc = XDocument.Load(URL);
    
                List<XElement> items = doc.Descendants("item").ToList();
            }
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-11
      • 1970-01-01
      • 1970-01-01
      • 2019-08-21
      • 2017-01-25
      • 1970-01-01
      • 1970-01-01
      • 2019-01-04
      相关资源
      最近更新 更多