从C#查看XML输出

时间:2014-06-09 14:17:15

标签: c# api ups

我正在尝试使用UPS API创建运输标签。 UPS api使用Web服务向UPS发送XML请求。然后UPS发回响应。这是我的问题。

有没有办法查看我调用“shipmentRequest”方法时输出的XML?

这是我第一次使用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 个答案:

答案 0 :(得分:2)

您可以通过覆盖GetWriterForMessage()提供自己的XmlWriter,从UPS服务继承并以xml的形式读取响应。您可以看到一个有效的示例here

答案 1 :(得分:1)

我正在使用此代码显示xml它可能会帮助你。

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