Просмотр вывода XML из C#

Я пытаюсь работать с API UPS для создания этикетки доставки. API api использует веб-сервис для отправки запроса XML в UPS. Затем UPS отправляет ответ обратно. Вот мой вопрос

Есть ли способ просмотреть XML, который выводится, когда я вызываю метод "shipmentRequest"?

Это первый раз, когда я использовал API и веб-сервис, поэтому, если вы хотите, чтобы я предоставил больше информации, просто дайте мне знать.

Спасибо!

РЕДАКТИРОВАТЬ: Вот мой код C#

                ShipService shpSvc = new ShipService();
            ShipmentRequest shipmentRequest = new ShipmentRequest();
            UPSSecurity upss = new UPSSecurity();
            //shpSvc.Url = "https://onlinetools.ups.com/webservices/Ship";
            UPSSecurityServiceAccessToken upssSvcAccessToken = new UPSSecurityServiceAccessToken();
            upssSvcAccessToken.AccessLicenseNumber = apiCode;
            upss.ServiceAccessToken = upssSvcAccessToken;
            UPSSecurityUsernameToken upssUsrNameToken = new UPSSecurityUsernameToken();
            upssUsrNameToken.Username = userName;
            upssUsrNameToken.Password = password;
            upss.UsernameToken = upssUsrNameToken;
            shpSvc.UPSSecurityValue = upss;
            RequestType request = new RequestType();
            String[] requestOption = { "nonvalidate" };
            request.RequestOption = requestOption;
            shipmentRequest.Request = request;
            ShipmentType shipment = new ShipmentType();
            shipment.Description = "Ship webservice example";
            ShipperType shipper = new ShipperType();
            shipper.ShipperNumber = accountNumber;
            PaymentInfoType paymentInfo = new PaymentInfoType();
            ShipmentChargeType shpmentCharge = new ShipmentChargeType();
            BillShipperType billShipper = new BillShipperType();
            billShipper.AccountNumber = accountNumber;
            shpmentCharge.BillShipper = billShipper;
            shpmentCharge.Type = "01";
            ShipmentChargeType[] shpmentChargeArray = { shpmentCharge };
            paymentInfo.ShipmentCharge = shpmentChargeArray;
            shipment.PaymentInformation = paymentInfo;
            ShipWSSample.ShipWebReference.ShipAddressType shipperAddress = new ShipWSSample.ShipWebReference.ShipAddressType();
            String[] addressLine = { "480 Parkton Plaza" };
            shipperAddress.AddressLine = addressLine;
            shipperAddress.City = "Timonium";
            shipperAddress.PostalCode = "21093";
            shipperAddress.StateProvinceCode = "MD";
            shipperAddress.CountryCode = "US";
            shipperAddress.AddressLine = addressLine;
            shipper.Address = shipperAddress;
            shipper.Name = "ABC Associates";
            shipper.AttentionName = "ABC Associates";
            ShipPhoneType shipperPhone = new ShipPhoneType();
            shipperPhone.Number = "1234567890";
            shipper.Phone = shipperPhone;
            shipment.Shipper = shipper;
            ShipFromType shipFrom = new ShipFromType();
            ShipWSSample.ShipWebReference.ShipAddressType shipFromAddress = new ShipWSSample.ShipWebReference.ShipAddressType();
            String[] shipFromAddressLine = { "Ship From Street" };
            shipFromAddress.AddressLine = addressLine;
            shipFromAddress.City = "Timonium";
            shipFromAddress.PostalCode = "21093";
            shipFromAddress.StateProvinceCode = "MD";
            shipFromAddress.CountryCode = "US";
            shipFrom.Address = shipFromAddress;
            shipFrom.AttentionName = "Mr.ABC";
            shipFrom.Name = "ABC Associates";
            shipment.ShipFrom = shipFrom;
            ShipToType shipTo = new ShipToType();
            ShipToAddressType shipToAddress = new ShipToAddressType();
            String[] addressLine1 = { "Some Street" };
            shipToAddress.AddressLine = addressLine1;
            shipToAddress.City = "Roswell";
            shipToAddress.PostalCode = "30076";
            shipToAddress.StateProvinceCode = "GA";
            shipToAddress.CountryCode = "US";
            shipTo.Address = shipToAddress;
            shipTo.AttentionName = "DEF";
            shipTo.Name = "DEF Associates";
            ShipPhoneType shipToPhone = new ShipPhoneType();
            shipToPhone.Number = "1234567890";
            shipTo.Phone = shipToPhone;
            shipment.ShipTo = shipTo;
            ServiceType service = new ServiceType();
            service.Code = "01";
            shipment.Service = service;
            PackageType package = new PackageType();
            PackageWeightType packageWeight = new PackageWeightType();
            packageWeight.Weight = "1";
            ShipUnitOfMeasurementType uom = new ShipUnitOfMeasurementType();
            uom.Code = "LBS";
            packageWeight.UnitOfMeasurement = uom;
            package.PackageWeight = packageWeight;
            PackagingType packType = new PackagingType();
            packType.Code = "02";
            package.Packaging = packType;
            PackageType[] pkgArray = { package };
            shipment.Package = pkgArray;
            LabelSpecificationType labelSpec = new LabelSpecificationType();
            LabelStockSizeType labelStockSize = new LabelStockSizeType();
            labelStockSize.Height = "6";
            labelStockSize.Width = "4";
            labelSpec.LabelStockSize = labelStockSize;
            LabelImageFormatType labelImageFormat = new LabelImageFormatType();
            labelImageFormat.Code = "SPL";
            labelSpec.LabelImageFormat = labelImageFormat;
            shipmentRequest.LabelSpecification = labelSpec;
            shipmentRequest.Shipment = shipment;
            ShipmentResponse shipmentResponse = shpSvc.ProcessShipment(shipmentRequest);
            MessageBox.Show("The transaction was a " + shipmentResponse.Response.ResponseStatus.Description);
            MessageBox.Show("The 1Z number of the new shipment is " + shipmentResponse.ShipmentResults.ShipmentIdentificationNumber);

2 ответа

Решение

Вы можете наследовать от службы UPS и читать ответ как XML, предоставляя свой собственный XmlWriter, переопределяя GetWriterForMessage(), Вы можете увидеть рабочий пример здесь.

Я использую этот код отображения XML, это может помочь вам.

        XDocument mySourceDoc = new XDocument();
        mySourceDoc = XDocument.Load(shipmentResponse);
        txtxml.Text = mySourceDoc.ToString();
Другие вопросы по тегам