我不是 .NET 开发人员,但有一个简单(或者应该很简单!)的任务要完成。我基本上需要从各种字符串值生成一大块 XML,并将其格式化为 C# 中的有效 XML 对象。我的代码是这样的:
private XDocument BuildPayload(string weight, string fromCountryCode, string fromZipCode, string toCountryCode, string toZipCode, string serviceTypeCode, string packageTypeCode, string pickupTypeCode)
{
string upsAccessLiscenseNumber = "xxxxxxxxxxxxxxxx";
string upsUserID = "xxxxxxxxxx";
string upsPassword = "xxxxxxxxx";
string unitOfMeasurementWeight = "LBS";
StringBuilder sb = new StringBuilder();
sb.Clear();
sb.Append("<?xml version='1.0'?>");
sb.Append(" <AccessRequest xml:lang='en-US'>");
sb.Append(" <AccessLicenseNumber>").Append(upsAccessLiscenseNumber).Append("</AccessLicenseNumber>");
sb.Append(" <UserId>").Append(upsUserID).Append("</UserId>");
sb.Append(" <Password>").Append(upsPassword).Append("</Password>");
sb.Append(" </AccessRequest>");
sb.Append("<?xml version='1.0'?>");
sb.Append(" <RatingServiceSelectionRequest xml:lang='en-US'>");
sb.Append(" <Request>");
sb.Append(" <TransactionReference>");
sb.Append(" <CustomerContext>Rating and Service</CustomerContext>");
sb.Append(" <XpciVersion>1.0001</XpciVersion>");
sb.Append(" </TransactionReference>");
sb.Append(" <RequestAction>Rate</RequestAction>");
sb.Append(" <RequestOption>Shop</RequestOption>");
sb.Append(" </Request>");
sb.Append(" <PickupType>");
sb.Append(" <Code>").Append(pickupTypeCode).Append("</Code>");
sb.Append(" </PickupType>");
sb.Append(" <Shipment>");
sb.Append(" <Shipper>");
sb.Append(" <Address>");
sb.Append(" <PostalCode>").Append(fromZipCode).Append("</PostalCode>");
sb.Append(" <CountryCode>").Append(fromCountryCode).Append("</CountryCode>");
sb.Append(" </Address>");
sb.Append(" </Shipper>");
sb.Append(" <ShipTo>");
sb.Append(" <Address>");
sb.Append(" <PostalCode>").Append(toZipCode).Append("</PostalCode>");
sb.Append(" <CountryCode>").Append(toCountryCode).Append("</CountryCode>");
sb.Append(" </Address>");
sb.Append(" </ShipTo>");
sb.Append(" <Service>");
sb.Append(" <Code>").Append(serviceTypeCode).Append("</Code>");
sb.Append(" </Service>");
sb.Append(" <Package>");
sb.Append(" <PackagingType>");
sb.Append(" <Code>").Append(packageTypeCode).Append("</Code>");
//sb.Append(" <Description>Package</Description>");
sb.Append(" </PackagingType>");
sb.Append(" <Description>Rate Shopping</Description>");
sb.Append(" <PackageWeight>");
sb.Append(" <UnitOfMeasurement>");
sb.Append(" <Code>").Append(unitOfMeasurementWeight).Append("</Code>");
sb.Append(" </UnitOfMeasurement>");
sb.Append(" <Weight>").Append(weight).Append("</Weight>");
sb.Append(" </PackageWeight>");
sb.Append(" </Package>");
sb.Append(" <ShipmentServiceOptions/>");
sb.Append(" </Shipment>");
sb.Append("</RatingServiceSelectionRequest>");
XDocument doc = XDocument.Parse(sb.ToString());
return doc;
}
但是,当我调用它时,我得到一个错误:
Unexpected XML declaration. The XML declaration must be the first node in the document, and no white space characters are allowed to appear before it.
我发现了几篇建议一些事情的帖子(比如把它sb.Clear()
放在那里),但没有任何效果。似乎认为 XML 字符串的顶部有一些空间,但我看不到有。我能看到的唯一有点狡猾的是那里有两条<?xml version='1.0'?>
线。不过,这来自 UPS 文档,而且我有一个旧的 Classic ASP 应用程序,它使用几乎完全相同的 XML,并且工作正常,所以我不确定这是否是问题所在。
谁能指出我正确的方向?
非常感谢。