コード例 #1
0
        private ShippingOptionCollection ParseResponse(string response, ref string error)
        {
            ShippingOptionCollection shippingOptions = new ShippingOptionCollection();

            using (StringReader sr = new StringReader(response))
                using (XmlTextReader tr = new XmlTextReader(sr))


                    do
                    // while (tr.Read())
                    {
                        // Read the next XML record
                        tr.Read();

                        if ((tr.Name == "Error") && (tr.NodeType == XmlNodeType.Element))
                        {
                            string errorText = "";
                            while (tr.Read())
                            {
                                if ((tr.Name == "Description") && (tr.NodeType == XmlNodeType.Element))
                                {
                                    errorText += "Error Desc: " + tr.ReadString();
                                }
                                if ((tr.Name == "HelpContext") && (tr.NodeType == XmlNodeType.Element))
                                {
                                    errorText += "USPS Help Context: " + tr.ReadString() + ". ";
                                }
                            }
                            error = "USPS Error returned: " + errorText;
                        }

                        // Process the inner postage XML
                        if ((tr.Name == "Postage") && (tr.NodeType == XmlNodeType.Element))
                        {
                            string serviceCode = "";
                            string postalRate  = "";

                            // while (tr.Read())
                            do
                            {
                                tr.Read();

                                if ((tr.Name == "MailService") && (tr.NodeType == XmlNodeType.Element))
                                {
                                    serviceCode = tr.ReadString();

                                    tr.ReadEndElement();
                                    if ((tr.Name == "MailService") && (tr.NodeType == XmlNodeType.EndElement))
                                    {
                                        break;
                                    }
                                }
                                // if (((tr.Name == "Postage") && (tr.NodeType == XmlNodeType.EndElement)) || ((tr.Name == "Postage") && (tr.NodeType == XmlNodeType.Element)))
                                //   break;

                                if ((tr.Name == "Rate") && (tr.NodeType == XmlNodeType.Element))
                                {
                                    postalRate = tr.ReadString();
                                    tr.ReadEndElement();
                                    if ((tr.Name == "Rate") && (tr.NodeType == XmlNodeType.EndElement))
                                    {
                                        break;
                                    }
                                }
                            } while (!((tr.Name == "Postage") && (tr.NodeType == XmlNodeType.EndElement)));

                            if (shippingOptions.Find((s) => s.Name == serviceCode) == null)
                            {
                                ShippingOption shippingOption = new ShippingOption();
                                //TODO check whether we need to multiply rate by package quantity
                                shippingOption.Rate = Convert.ToDecimal(postalRate, new CultureInfo("en-US"));
                                shippingOption.Name = serviceCode;
                                shippingOptions.Add(shippingOption);
                            }
                        }
                    } while (!tr.EOF);
            return(shippingOptions);
        }
コード例 #2
0
        private ShippingOptionCollection ParseResponse(string response, ref string error)
        {
            ShippingOptionCollection shippingOptions = new ShippingOptionCollection();

            using (StringReader sr = new StringReader(response))
            using (XmlTextReader tr = new XmlTextReader(sr))


                do
                // while (tr.Read())
                {
                    // Read the next XML record
                    tr.Read();

                    if ((tr.Name == "Error") && (tr.NodeType == XmlNodeType.Element))
                    {
                        string errorText = "";
                        while (tr.Read())
                        {
                            if ((tr.Name == "Description") && (tr.NodeType == XmlNodeType.Element))
                                errorText += "Error Desc: " + tr.ReadString();
                            if ((tr.Name == "HelpContext") && (tr.NodeType == XmlNodeType.Element))
                                errorText += "USPS Help Context: " + tr.ReadString() + ". ";
                        }
                        error = "USPS Error returned: " + errorText;
                    }

                    // Process the inner postage XML
                    if ((tr.Name == "Postage") && (tr.NodeType == XmlNodeType.Element))
                    {
                        string serviceCode = "";
                        string postalRate = "";

                        // while (tr.Read())
                        do
                        {

                            tr.Read();

                            if ((tr.Name == "MailService") && (tr.NodeType == XmlNodeType.Element))
                            {
                                serviceCode = tr.ReadString();

                                tr.ReadEndElement();
                                if ((tr.Name == "MailService") && (tr.NodeType == XmlNodeType.EndElement))
                                    break;
                            }
                            // if (((tr.Name == "Postage") && (tr.NodeType == XmlNodeType.EndElement)) || ((tr.Name == "Postage") && (tr.NodeType == XmlNodeType.Element)))
                            //   break;

                            if ((tr.Name == "Rate") && (tr.NodeType == XmlNodeType.Element))
                            {
                                postalRate = tr.ReadString();
                                tr.ReadEndElement();
                                if ((tr.Name == "Rate") && (tr.NodeType == XmlNodeType.EndElement))
                                    break;
                            }

                        } while (!((tr.Name == "Postage") && (tr.NodeType == XmlNodeType.EndElement)));

                        if (shippingOptions.Find((s) => s.Name == serviceCode) == null)
                        {
                            ShippingOption shippingOption = new ShippingOption();
                            //TODO check whether we need to multiply rate by package quantity
                            shippingOption.Rate = Convert.ToDecimal(postalRate, new CultureInfo("en-US"));
                            shippingOption.Name = serviceCode;
                            shippingOptions.Add(shippingOption);
                        }
                    }
                } while (!tr.EOF);
            return shippingOptions;
        }
コード例 #3
0
        private ShippingOptionCollection ParseResponse(string response, bool isDomestic, ref string error)
        {
            var shippingOptions = new ShippingOptionCollection();

            string postageStr     = isDomestic ? "Postage" : "Service";
            string mailServiceStr = isDomestic ? "MailService" : "SvcDescription";
            string rateStr        = isDomestic ? "Rate" : "Postage";

            using (var sr = new StringReader(response))
                using (var tr = new XmlTextReader(sr))
                {
                    do
                    {
                        tr.Read();

                        if ((tr.Name == "Error") && (tr.NodeType == XmlNodeType.Element))
                        {
                            string errorText = "";
                            while (tr.Read())
                            {
                                if ((tr.Name == "Description") && (tr.NodeType == XmlNodeType.Element))
                                {
                                    errorText += "Error Desc: " + tr.ReadString();
                                }
                                if ((tr.Name == "HelpContext") && (tr.NodeType == XmlNodeType.Element))
                                {
                                    errorText += "USPS Help Context: " + tr.ReadString() + ". ";
                                }
                            }
                            error = "USPS Error returned: " + errorText;
                        }

                        if ((tr.Name == postageStr) && (tr.NodeType == XmlNodeType.Element))
                        {
                            string serviceCode = string.Empty;
                            string postalRate  = string.Empty;

                            do
                            {
                                tr.Read();

                                if ((tr.Name == mailServiceStr) && (tr.NodeType == XmlNodeType.Element))
                                {
                                    serviceCode = tr.ReadString();

                                    tr.ReadEndElement();
                                    if ((tr.Name == mailServiceStr) && (tr.NodeType == XmlNodeType.EndElement))
                                    {
                                        break;
                                    }
                                }

                                if ((tr.Name == rateStr) && (tr.NodeType == XmlNodeType.Element))
                                {
                                    postalRate = tr.ReadString();
                                    tr.ReadEndElement();
                                    if ((tr.Name == rateStr) && (tr.NodeType == XmlNodeType.EndElement))
                                    {
                                        break;
                                    }
                                }
                            }while (!((tr.Name == postageStr) && (tr.NodeType == XmlNodeType.EndElement)));

                            if (shippingOptions.Find((s) => s.Name == serviceCode) == null)
                            {
                                var shippingOption = new ShippingOption();
                                //TODO check whether we need to multiply rate by package quantity
                                shippingOption.Rate = Convert.ToDecimal(postalRate, new CultureInfo("en-US"));
                                shippingOption.Name = serviceCode;
                                shippingOptions.Add(shippingOption);
                            }
                        }
                    }while (!tr.EOF);
                }
            return(shippingOptions);
        }