非空白字符无法添加到XDocument和UPS API的内容中

时间:2016-04-05 15:52:02

标签: c# xml ups

我尝试使用xDocument在xml中发送UPSAPI请求。但由于某种原因我甚至试图使xml错误?我得到错误:

无法将空白字符添加到内容中。

在字符串生成器本身上,并且在任何特定区域。所以我不知道它失败了。这是代码:

        var requestXml = new XDocument(
            new XDeclaration("1.0", "utf-16", "yes"),
            new XElement("AccessRequest",
                new XAttribute(XNamespace.Xml + "lang", "en"),
                new XElement("AccessLicenseNumber", "LICENCE"),
                new XElement("UserId", "USERID"),
                new XElement("Password", "PASSWORD")),

            new XDeclaration("1.0", "utf-16", "yes"),
            new XElement("RatingServiceSelectionRequest",
                new XAttribute(XNamespace.Xml + "lang", "en"),
                new XElement("Request",
                    new XElement("TransactionReference",
                        new XElement("CustomerContext", "Rating and Service"),
                        new XElement("XpciVersion", "1.0001")),
                    new XElement("RequestAction", "Rate"),
                    new XElement("RequestOption", "Rate")),

                new XElement("PickupType",
                    new XElement("Code", "01"),
                    new XElement("Description", "Daily Pickup")),

                new XElement("Shipment",
                    new XElement("Shipper",
                        new XElement("Address",
                            new XElement("PostalCode", "33706"),
                            new XElement("CountryCode", "US"))),

                    new XElement("ShipTo",
                        new XElement("Address",
                            new XElement("ResidentialAddressIndicator", ""), //GET from postObject
                            new XElement("PostalCode", "34221"), //GET from postObject
                            new XElement("CountryCode", "US"))), //GET from postObject

                    new XElement("ShipFrom",
                        new XElement("Address",
                            new XElement("PostalCode", "33706"),
                            new XElement("CountryCode", "US"))),

                    new XElement("Service",
                        new XElement("Code", "03")), //GET from postObject

                    new XElement("Package",
                        new XElement("PackagingType",
                            new XElement("Code", "02")),

                        new XElement("Dimensions",
                            new XElement("UnitOfMeasurement",
                                new XElement("Code", "IN")),
                            new XElement("Length", "20"),
                            new XElement("Width", "20"),
                            new XElement("Height", "20")),

                        new XElement("PackageWeight",
                            new XElement("UnitOfMeasurement",
                                new XElement("Code", "LBS")),
                            new XElement("Weight", "10")))))); //GET from postObject

更新

好,所以消除了大部分代码我得到了错误:

此操作会创建一个结构不正确的文档。

用这个:

        var requestXml = new XDocument(
            new XDeclaration("1.0", "utf-8", "yes"),
            new XElement("AccessRequest",
                new XAttribute(XNamespace.Xml + "lang", "en"),
                new XElement("AccessLicenseNumber", "LICENCE"),
                new XElement("UserId", "USERID"),
                new XElement("Password", "PASSWORD")),
            new XElement("RatingServiceSelectionRequest", "this"));

字面意思是:

    <?xml version="1.0" encoding="UTF-16"?>
    <AccessRequest xml:lang="en-US">
           <AccessLicenseNumber>LICENCE</AccessLicenseNumber>
           <UserId>USERID</UserId>
           <Password>PASSWORD</Password>
    </AccessRequest>
    <RatingServiceSelectionRequest>this</RatingServiceSelectionRequest>

怎么没有正确的结构?我错过了什么吗?

1 个答案:

答案 0 :(得分:0)

XDocument不允许多个root如上所述。但UPS运输API需要两个根用于许可证验证请求,另一个用于计算请求。因此我不得不将两者分成字符串:

# To delete a remote branch use:
git push origin --delete <branch name> 

# to delete local branch (force delete) use:
git branch -D <branch name>

现在一切正常。