The World’s Largest Online Community for Developers

'; How to filter SOAP results client side in C#? - LavOzs.Com

I make SOAP requests to an e-commerce API to know how many orders were made for a specified product, in a given time frame.

My method takes a product ID and a number of days to create a time frame. Then it constructs a SOAP request and returns the amount sold products.

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("");
            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;
                                } catch (IndexOutOfRangeException ex) {
                                    // error is thrown to escape loop - sloppy, I know
                        orderedCountToPass = (int)orderedCount;
                        orderedCount = 0;
                } catch (NullReferenceException) { 
                    // do nothing, we just want to exit while loop when i is higher than page count
                return orderedCountToPass;

The result often should be in hundreds, but regardless how well a product sells, it returns something from 0 to 4.

Here is a sample response:

Sample SOAP response

For example, I'm only interested with productId == 479, but an order was made with other products as well that don't interest me - I need to filter them.

I'm doing something wrong with how I try to filter results. How do I do it properly? I'm certain the request is correct and response does contain all possible orders.

You are getting xml results so use Net Xml library :

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();

In C#, how do I calculate someone's age based on a DateTime type birthday?
How do I create an Excel (.XLS and .XLSX) file in C# without installing Microsoft Office?
How To: Execute command line in C#, get STD OUT results
How do I get a consistent byte representation of strings in C# without manually specifying an encoding?
How to pass “Null” (a real surname!) to a SOAP web service in ActionScript 3
How do I turn a C# object into a JSON string in .NET?
How to call asynchronous method from synchronous method in C#?