I'm not a .NET dev but have a simple (or should be simple!) task to accomplish. I basically need to generate a chunk of XML from various string values and format this into a valid XML object in C#. The code I have is like this:\[code\]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; }\[/code\]However, when I call this I get an error:\[code\]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.\[/code\]I've found a couple of posts suggesting things (like putting the \[code\]sb.Clear()\[/code\] in there) but nothing works. It seems to think there's some space at the top of the XML string, but I can't see that there is. The only slightly dodgy thing I can see is that there are two \[code\]<?xml version='1.0'?>\[/code\] lines in there. This has come from the UPS docs though, and I have an old Classic ASP app that uses pretty much this exact same XML and it works fine, so I'm not sure that's the problem.Can anyone point me in the right direction with this please?Many thanks.