c# XML到CSV的API结果
本文关键字:API 结果 CSV XML | 更新日期: 2023-09-27 18:04:06
我有一个连接到api获取订单并将xml写入文件的脚本。
我该如何将其写入CSV呢?
下面是示例代码
private void button1_Click(object sender, EventArgs e)
{
string developerKey = string.Empty;
string password = string.Empty;
string accountID = string.Empty;
developerKey = "xxxxx";
password= "xxxxx";
accountID = "xxxxx";
OrderCriteria criteria = new OrderCriteria();
criteria.StatusUpdateFilterBeginTimeGMT = DateTime.UtcNow.AddDays(-30);
criteria.StatusUpdateFilterEndTimeGMT = DateTime.UtcNow;
// this would be like a Ready-To-Ship Report
criteria.PaymentStatusFilter = "Cleared";
criteria.ShippingStatusFilter = "Unshipped";
criteria.ExportState = "NotExported";
criteria.DetailLevel = "Complete";
criteria.PageSize = 20;
OrderService orderService = new OrderService();
orderService.APICredentialsValue = new APICredentials();
orderService.APICredentialsValue.DeveloperKey = developerKey;
orderService.APICredentialsValue.Password = password;
try
{
criteria.PageNumberFilter = 1;
while(true)
{
APIResultOfArrayOfOrderResponseItem orderResponse = orderService.GetOrderList(accountID, criteria);
if (orderResponse.Status == ResultStatus.Failure)
{
throw new Exception(orderResponse.Message);
}
if (orderResponse.ResultData.Length == 0)
break; // we moved outside the pages that have data on them
List<string> clientOrderIdentifiers = new List<string>();
XmlSerializer serializer = new XmlSerializer(typeof(OrderResponseDetailComplete));
XmlTextWriter writer = null;
foreach(OrderResponseItem order in orderResponse.ResultData)
{
OrderResponseDetailComplete completeOrder = order as OrderResponseDetailComplete;
//put the order on the disk as its own file
using (writer = new XmlTextWriter(string.Format(@"C:'orders'{0}.xml", completeOrder.ClientOrderIdentifier), System.Text.Encoding.UTF8))
{
serializer.Serialize(writer, completeOrder);
}
//keep the ClientOrderIdentifier so we can send a batch to mark the orders as exported, filtering the orders from future queries
clientOrderIdentifiers.Add(completeOrder.ClientOrderIdentifier);
}
//submit our batch of processed orders
APIResultOfArrayOfBoolean exportStatusResponse = orderService.SetOrdersExportStatus(accountID, clientOrderIdentifiers.ToArray(), true);
if (exportStatusResponse.Status == ResultStatus.Failure)
{
throw new Exception(exportStatusResponse.Message);
}
//make sure each order was succesfully updated
for (int responseCount = 0; responseCount < exportStatusResponse.ResultData.Length; ++responseCount)
{
if (exportStatusResponse.ResultData[responseCount] == false)
{
//order was not successfully marked as exported
MessageBox.Show("Order was not successfully marked as exported:" + orderResponse.ResultData[responseCount].ClientOrderIdentifier);
}
}
++criteria.PageNumberFilter;
}
}
catch (Exception ex)
{ // big catch for the entire function
MessageBox.Show(ex.ToString());
}
}
我只需要一些xml。
目前正在下载每个订单并将其写入单独的文件,我只是希望所有这些都在一个理想的文件中。
谢谢
你可以使用Service Stack, . net最快的JSON, JSON和CSV文本序列化器(更多信息)或其他库,如CsvHelper。
对于只序列化您感兴趣的订单,首先使用linq:
选择它们。 var orders = orderResponse
.Select(order => order.SomeProperty && ..)
orders.ToCsv().Print();