Exemple #1
0
        public static USPSRateResponse FromXml(string xml)
        {
            USPSRateResponse r = new USPSRateResponse();
            int idx1           = 0;
            int idx2           = 0;

            if (xml.Contains("<ZipOrigination>"))
            {
                idx1        = xml.IndexOf("<ZipOrigination>") + 17;
                idx2        = xml.IndexOf("</ZipOrigination>");
                r.OriginZip = xml.Substring(idx1, idx2 - idx1);
            }

            if (xml.Contains("<ZipDestination>"))
            {
                idx1        = xml.IndexOf("<ZipDestination>") + 16;
                idx2        = xml.IndexOf("</ZipDestination>");
                r.OriginZip = xml.Substring(idx1, idx2 - idx1);
            }

            if (xml.Contains("<Pounds>"))
            {
                idx1        = xml.IndexOf("<Pounds>") + 8;
                idx2        = xml.IndexOf("</Pounds>");
                r.OriginZip = xml.Substring(idx1, idx2 - idx1);
            }

            if (xml.Contains("<Ounces>"))
            {
                idx1        = xml.IndexOf("<Ounces>") + 8;
                idx2        = xml.IndexOf("</Ounces>");
                r.OriginZip = xml.Substring(idx1, idx2 - idx1);
            }

            if (xml.Contains("<Container>"))
            {
                idx1        = xml.IndexOf("<Container>") + 11;
                idx2        = xml.IndexOf("</Container>");
                r.OriginZip = xml.Substring(idx1, idx2 - idx1);
            }

            if (xml.Contains("<Size>"))
            {
                idx1        = xml.IndexOf("<Size>") + 6;
                idx2        = xml.IndexOf("</Size>");
                r.OriginZip = xml.Substring(idx1, idx2 - idx1);
            }

            if (xml.Contains("<Zone>"))
            {
                idx1        = xml.IndexOf("<Zone>") + 6;
                idx2        = xml.IndexOf("</Zone>");
                r.OriginZip = xml.Substring(idx1, idx2 - idx1);
            }

            int lastidx = 0;

            while (xml.IndexOf("<MailService>", lastidx) > -1)
            {
                Postage p = new Postage();
                idx1 = xml.IndexOf("<MailService>", lastidx) + 13;
                idx2 = xml.IndexOf("</MailService>", lastidx + 13);
                p.ShippingMethodName = xml.Substring(idx1, idx2 - idx1);

                idx1           = xml.IndexOf("<Rate>", lastidx) + 6;
                idx2           = xml.IndexOf("</Rate>", lastidx + 13);
                p.TotalCharges = Decimal.Parse(xml.Substring(idx1, idx2 - idx1));
                r.Postage.Add(p);
                lastidx = idx2;
            }
            return(r);
        }
        public List<USPSRateResponse> ParseRate(XmlDocument rateresponse)
        {
            List<USPSRateResponse> response = new List<USPSRateResponse>();

            USPSRateResponse ratedetail = new USPSRateResponse();
            var elem = rateresponse.DocumentElement;
            ratedetail.IsDomestic = true;
            if (rateresponse.DocumentElement != null)
            {
                XmlNodeList list = rateresponse.DocumentElement.SelectNodes("Package");
                if (list != null)
                    foreach (XmlNode node in list)
                    {
                        if (node.SelectSingleNode("ZipOrigination") != null)
                            ratedetail.OriginZip = node.SelectSingleNode("ZipOrigination").InnerXml;
                        if (node.SelectSingleNode("ZipDestination") != null)
                            ratedetail.DestZip = node.SelectSingleNode("ZipDestination").InnerXml;
                        if (node.SelectSingleNode("Pounds") != null)
                            ratedetail.Pounds = decimal.Parse(node.SelectSingleNode("Pounds").InnerXml);
                        if (node.SelectSingleNode("Ounces") != null)
                            ratedetail.Ounces = decimal.Parse(node.SelectSingleNode("Ounces").InnerXml);
                        if (node.SelectSingleNode("Container") != null)
                            ratedetail.Container = node.SelectSingleNode("Container").InnerXml;
                        if (node.SelectSingleNode("Size") != null)
                            ratedetail.Size = node.SelectSingleNode("Size").InnerXml;
                        if (node.SelectSingleNode("Zone") != null)
                            ratedetail.Zone = node.SelectSingleNode("Zone").InnerXml;


                        XmlNodeList postagelist = node.SelectNodes("Postage");
                        if (postagelist != null)
                            foreach (XmlNode postage in postagelist)
                            {
                                var p = new Postage();
                                if (postage.SelectSingleNode("MailService") != null)
                                {
                                    string serv = "";
                                    Services.TryGetValue(postage.SelectSingleNode("MailService").InnerXml.ToString(), out serv);
                                    p.ShippingMethodName = serv;
                                }

                                if (postage.SelectSingleNode("Rate") != null)
                                    p.TotalCharges = decimal.Parse(postage.SelectSingleNode("Rate").InnerXml);
                                ratedetail.Postage.Add(p);
                            }
                        response.Add(ratedetail);
                    }
            }

            return response;
        }
        public static USPSRateResponse FromXml(string xml)
        {
            USPSRateResponse r = new USPSRateResponse();
            int idx1 = 0;
            int idx2 = 0;

            if (xml.Contains("<ZipOrigination>"))
            {
                idx1 = xml.IndexOf("<ZipOrigination>") + 17;
                idx2 = xml.IndexOf("</ZipOrigination>");
                r.OriginZip = xml.Substring(idx1, idx2 - idx1);
            }

            if (xml.Contains("<ZipDestination>"))
            {
                idx1 = xml.IndexOf("<ZipDestination>") + 16;
                idx2 = xml.IndexOf("</ZipDestination>");
                r.OriginZip = xml.Substring(idx1, idx2 - idx1);
            }

            if (xml.Contains("<Pounds>"))
            {
                idx1 = xml.IndexOf("<Pounds>") + 8;
                idx2 = xml.IndexOf("</Pounds>");
                r.OriginZip = xml.Substring(idx1, idx2 - idx1);
            }

            if (xml.Contains("<Ounces>"))
            {
                idx1 = xml.IndexOf("<Ounces>") + 8;
                idx2 = xml.IndexOf("</Ounces>");
                r.OriginZip = xml.Substring(idx1, idx2 - idx1);
            }

            if (xml.Contains("<Container>"))
            {
                idx1 = xml.IndexOf("<Container>") + 11;
                idx2 = xml.IndexOf("</Container>");
                r.OriginZip = xml.Substring(idx1, idx2 - idx1);
            }

            if (xml.Contains("<Size>"))
            {
                idx1 = xml.IndexOf("<Size>") + 6;
                idx2 = xml.IndexOf("</Size>");
                r.OriginZip = xml.Substring(idx1, idx2 - idx1);
            }

            if (xml.Contains("<Zone>"))
            {
                idx1 = xml.IndexOf("<Zone>") + 6;
                idx2 = xml.IndexOf("</Zone>");
                r.OriginZip = xml.Substring(idx1, idx2 - idx1);
            }

            int lastidx = 0;
            while (xml.IndexOf("<MailService>", lastidx) > -1)
            {
                Postage p = new Postage();
                idx1 = xml.IndexOf("<MailService>", lastidx) + 13;
                idx2 = xml.IndexOf("</MailService>", lastidx + 13);
                p.ShippingMethodName = xml.Substring(idx1, idx2 - idx1);

                idx1 = xml.IndexOf("<Rate>", lastidx) + 6;
                idx2 = xml.IndexOf("</Rate>", lastidx + 13);
                p.TotalCharges = Decimal.Parse(xml.Substring(idx1, idx2 - idx1));
                r.Postage.Add(p);
                lastidx = idx2;
            }
            return r;
        }