上下文:C#,Xero.Api
我和Invoices度过了一段有趣的时光。我编写了一些C#代码来接受命令行中的查询字符串和文件名。我针对Xero的端点运行查询并将结果存储在文件中。到目前为止一切都很好。
然而,我发现在某些情况下我会得到异常结果。
我正在使用以下C#代码:
geom_text
我有一个包含以下内容的批处理文件:
private static void Main(string[] args)
{
if (args.Length != 2)
{
Console.WriteLine("Syntax:\n\tXero \"<querystring>\" <outputspec>");
Environment.Exit(1);
}
var query = args[0];
var fspec = args[1];
Console.WriteLine($"{query}\n{fspec}");
// Private Application Sample
var xeroCoreApi = new XeroCoreApi("https://api.xero.com",
new PrivateAuthenticator(<not shown>),
new Consumer(<not shown>, <not shown>),
null,
new DefaultMapper(),
new DefaultMapper());
var org = xeroCoreApi.Organisation;
var user = new ApiUser { Name = Environment.MachineName };
var invoices = xeroCoreApi
.Invoices
.Where(query)
.Find();
var total = invoices.Count();
var page = 2;
StringBuilder sb = new StringBuilder();
sb.Append("<Invoices>");
if (invoices.Any())
{
foreach (Invoice I in invoices)
{
GatherInvoiceDetails(sb, I);
}
total = invoices.Count();
while (invoices.Count() == 100)
{
invoices = xeroCoreApi.Invoices.Page(page++).Find();
if (invoices.Any())
{
total += invoices.Count();
foreach (Invoice I in invoices)
{
GatherInvoiceDetails(sb, I);
}
}
}
}
sb.AppendLine("</Invoices>");
Console.WriteLine($"pages: {page-1}. invoices: {total}");
System.IO.File.WriteAllText(fspec, sb.ToString());
}
运行批次给出:
@echo off
Xero.exe "DueDate >= DateTime(2017,08, 01) && DueDate <= DateTime(2017,09, 14) && Status == ""AUTHORISED""" \temp\authorised.xml
Xero.exe "DueDate >= DateTime(2017,08, 01) && DueDate <= DateTime(2017,09, 14) && Status == ""DELETED""" \temp\deleted.xml
Xero.exe "DueDate >= DateTime(2017,08, 01) && DueDate <= DateTime(2017,09, 14) && Status == ""DRAFT""" \temp\draft.xml
Xero.exe "DueDate >= DateTime(2017,08, 01) && DueDate <= DateTime(2017,09, 14) && Status == ""PAID""" \temp\paid.xml
Xero.exe "DueDate >= DateTime(2017,08, 01) && DueDate <= DateTime(2017,09, 14) && Status == ""VOIDED""" \temp\voided.xml
Xero.exe "DueDate >= DateTime(2017,08, 01) && DueDate <= DateTime(2017,09, 14)" \temp\all.xml
请注意,“all.xml”输出存储3339个发票。 “paid.xml”还存储了3339张发票。如果你进行算术运算,你会发现无效,付费,草稿,删除和授权的数字加起来大于3339。
这有点奇怪。有什么想法吗?
P.S。
同样奇怪的是,在all.xml中,有些项目的DueDate超出了请求范围,例如:
DueDate >= DateTime(2017,08, 01) && DueDate <= DateTime(2017,09, 14) && Status == "AUTHORISED"
\temp\authorised.xml
pages: 1. invoices: 32
DueDate >= DateTime(2017,08, 01) && DueDate <= DateTime(2017,09, 14) && Status == "DELETED"
\temp\deleted.xml
pages: 1. invoices: 0
DueDate >= DateTime(2017,08, 01) && DueDate <= DateTime(2017,09, 14) && Status == "DRAFT"
\temp\draft.xml
pages: 1. invoices: 2
DueDate >= DateTime(2017,08, 01) && DueDate <= DateTime(2017,09, 14) && Status == "PAID"
\temp\paid.xml
pages: 34. invoices: 3339
DueDate >= DateTime(2017,08, 01) && DueDate <= DateTime(2017,09, 14) && Status == "VOIDED"
\temp\voided.xml
pages: 1. invoices: 26
DueDate >= DateTime(2017,08, 01) && DueDate <= DateTime(2017,09, 14)
\temp\all.xml
pages: 34. invoices: 3339
P.P.S。
也许我只需绕过Xero过滤并自行完成,即
<DueDate>7/22/2016 12:00:00 AM</DueDate>
...
var everything = new System.Collections.Generic.List<Xero.Api.Core.Model.Invoice>();
...
foreach (Invoice I in invoices)
{
everything.Add(I);
}