-1

I've had code working for several years now and I had to upgrade the project to .Net 4. After upgrading the project, I am getting the following error after accessing any method:

There is an error in XML document (1,1) - hexadecimal value 0x1F, is an invalid character. Line 1 position 1.

Here is a sample of the code I'm attempting to run:

    Dim ConsumerKey As String = "XXXXXXXXXXXXXXXXXXXXXX"
        Dim UserAgentString As String = "TEST Xero Interface (Private App Testing)"
        Dim privateCertificate As X509Certificate2 = New X509Certificate2("C:\Data\public_privatekey.pfx", "xxxxxxx")
        Dim uConsumerSession As IOAuthSession = New XeroApiPrivateSession(UserAgentString, ConsumerKey, privateCertificate)
        Dim uRepository As New Repository(uConsumerSession)


        Dim uInvoice As New Model.Invoice
        Dim uContact As New Model.Contact
        uContact.Name = "ABC Ltd"
        uContact.IsCustomer = True
        uInvoice.Contact = uContact
        uInvoice.Type = "ACCREC"
        uInvoice.Date = Now
        uInvoice.DueDate = DateAdd(DateInterval.Day, 14, Now)
        uInvoice.Status = "DRAFT"
        uInvoice.LineAmountTypes = 0

        uInvoice.LineItems = New LineItems
        Dim uLineItem = New Model.LineItem
        uLineItem.Quantity = 1
        uLineItem.ItemCode = "TEST TRANS"
        uLineItem.Description = "Testing Router"
        uLineItem.AccountCode = "400"
        uLineItem.UnitAmount = 200
        uInvoice.LineItems.Add(uLineItem)
        uRepository.Create(Of Model.Invoice)(uInvoice)

What am I doing wrong here? Why did the same code I've used for years stop working?

TRaymond
  • 41
  • 3

1 Answers1

0

TLDR; Your code will be using TLS 1.0 which is deprecated - use the following in your code to force TLS 1.2

 ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

I am sure you have worked this out already but I was getting the same thing, API was returning an undocumented response code 412 so being rejected prior to even getting to the API and doing things,

The error response handling in that Older SDK which you look to be using for .NET was not working that well so was trying to parse the error response hence the error - which put me off for a bit while i debugged.

I decided to upgrade to the new Xeronet SDK which is actively being maintained - https://github.com/XeroAPI/Xero-Net

Then i got the same error except this time the error message was useful, stated that TLS 1.0 was not supported please upgrade, Ah. Ha... they turned support of for this end of June 2018 according to their site, I think it actually happened end of July as I had things working on this code at the end of July.

Anyway upgrade your code ideally to the new SDK and then force .net to use TLS 1.2 using the below if on .net 4.5

 ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

if using 4.0 then

ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;