mattc19
mattc19

Reputation: 718

Viewing XML output from C#

I'm trying to work with the UPS api to create a shipping label. The UPS api uses a webservice to send an XML request to UPS. UPS then sends a response back. Here is my question.

Is there a way to view the XML that is outputted when I call the "shipmentRequest" method?

This is the first time I've used an API and a webservice so if you need me to provide more information just let me know.

Thanks!

EDIT: Here is my C# code

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

Upvotes: 0

Views: 738

Answers (2)

KarthikManoharan
KarthikManoharan

Reputation: 805

i am using this code display xml it may help you.

        XDocument mySourceDoc = new XDocument();
        mySourceDoc = XDocument.Load(shipmentResponse);
        txtxml.Text = mySourceDoc.ToString();

Upvotes: 1

Michael G
Michael G

Reputation: 6755

You can inherit from the UPS service and read the response as xml by providing your own XmlWriter by overriding GetWriterForMessage(). You can see a working example here.

Upvotes: 2

Related Questions