查看c#的XML输出

本文关键字:输出 XML 查看 | 更新日期: 2023-09-27 18:07:36

我正在尝试使用UPS api创建一个运输标签。UPS api使用webservice向UPS发送XML请求。然后UPS发送一个响应。这是我的问题。

是否有一种方法可以查看当我调用"shipmentRequest"方法时输出的XML ?

这是我第一次使用API和web服务,所以如果你需要我提供更多的信息,请告诉我。

谢谢!

编辑:这是我的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);

查看c#的XML输出

您可以通过重写GetWriterForMessage()提供您自己的XmlWriter来继承UPS服务并以xml形式读取响应。您可以在这里看到一个工作示例:

我正在使用这个代码显示XML,它可能会对您有所帮助。

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