我需要一些解析CSV的帮助,我们就像这样
OrderNumber,LineNumber,CustomerNumber,CustomerName,AddressLine1,AddressLine2,AddressLine3,AddressLine4,AddressLine5,PostCode,ProductCode,ProductName,Ordered,Left,Picked
我们可以为订单设置多行,这样我们就可以获得
多次匹配我需要一种解析这个结构的方法
<orderDespatchRequest>
<despatchDetails>
<clientCustomerId>CustomerNumber</clientCustomerId>
<clientOrderNumber>OrderNumber</clientOrderNumber>
<dateDespatch>2015-07-01T00:00:00</dateDespatch>
<despatchedDetail>
<orderDespatchDetail>
<lineNumber>LineNumber</lineNumber>
<productCode>ProductCode</productCode>
<productName>ProductName</productName>
<quantity>Picked</quantity>
</orderDespatchDetail>
</despatchedDetail>
</despatchDetails>
</orderDespatchRequest>
希望有人能帮忙吗?
这是我到目前为止所得到的
public bool ExtractCSV(string file)
{
#region set up new dataTable
dt = new DataTable("Header");
dt.Columns.Add("OrderNumber", typeof(string));
dt.Columns.Add("Company", typeof(string));
dt.Columns.Add("AddressLine1", typeof(string));
dt.Columns.Add("AddressLine2", typeof(string));
dt.Columns.Add("AddressLine3", typeof(string));
dt.Columns.Add("AddressLine4", typeof(string));
dt.Columns.Add("AddressLine5", typeof(string));
dt.Columns.Add("PostCode", typeof(string));
detailTable = new DataTable("Details");
detailTable.Columns.Add("OrderNumber", typeof(string));
detailTable.Columns.Add("LineNumber", typeof(int));
detailTable.Columns.Add("ProductCode", typeof(string));
detailTable.Columns.Add("ProductName", typeof(string));
detailTable.Columns.Add("OrderQty", typeof(int));
detailTable.Columns.Add("OutstandingQty", typeof(int));
detailTable.Columns.Add("DespatchedQty", typeof(string));
detailTable.PrimaryKey = new DataColumn[] { detailTable.Columns["OrderNumber"] };
#endregion
#region Fill the table
// read in the csv file
string[] csvRows = File.ReadAllLines(file);
string[] fields = null;
foreach (string csvRow in csvRows)
{
fields = csvRow.Split(',');
string orderNumber = fields[0].ToString();
string customerNumber = fields[2].ToString();
string Company = fields[3].ToString();
string AddressLine1 = fields[4].ToString();
string AddressLine2 = fields[5].ToString();
string AddressLine3 = fields[6].ToString();
string AddressLine4 = fields[7].ToString();
string AddressLine5 = fields[8].ToString();
string PostCode = fields[9].ToString();
int LineNumber = Convert.ToInt32(fields[1]);
string ProductCode = fields[10].ToString();
string ProductName = fields[11].ToString();
int OrderQty = Convert.ToInt32(fields[12]);
int OutstandingQty = Convert.ToInt32(fields[13]);
int DespatchedQty = Convert.ToInt32(fields[14]);
dt.Rows.Add(orderNumber, Company, AddressLine1, AddressLine2, AddressLine3, AddressLine4, AddressLine5,PostCode);
detailTable.Rows.Add(orderNumber, ProductCode, ProductName, OrderQty, OutstandingQty, DespatchedQty);
}
#endregion
var query = from row in detailTable.AsEnumerable()
group row by row.Field<string>("OrderNumber") into grp
select new DataClass()
{
OrderNumber = grp.Key,
Details = grp
};
OrderDespatchDetail detail = new OrderDespatchDetail();
DespatchDetails despatchDetail = new DespatchDetails();
string orderNo = string.Empty;
string custNo = string.Empty;
foreach (DataRow item in query)
{
DataRow found = dt.Rows.Find(item.Field<string>("OrderNumber"));
if (orderNo != found.Field<string>("OrderNumber"))
{
}
detail.LineNumber = item.Field<int>("LineNumber");
detail.ProductCode = item.Field<string>("ProductCode");
detail.ProductName = item.Field<string>("ProductName");
detail.Quantity = item.Field<int>("");
}
OrderDespatchRequest request = new OrderDespatchRequest();
request.despatchDetails = despatchDetail;
return SendOrderDespatch(request);
}
可以顺其自然。 此致
艾丹
答案 0 :(得分:0)
作为一个例子,以下内容应该让你开始。
阅读CSV文件
var rows = (from row in File.ReadAllLines("Test.csv")
let item = row.Split(',')
select new
{
OrderNumber = item[0],
LineNumber = item[1],
CustomerID = item[2],
Company = item[3],
AddressLine1 = item[4],
AddressLine2 = item[5],
AddressLine3 = item[6],
AddressLine4 = item[7],
AddressLine5 = item[8],
PostCode = item[9],
ProductCode = item[10].ToString(),
ProductName = item[11].ToString(),
OrderQty = item[12],
OutstandingQty = item[13],
DespatchedQty = item[14]
}).ToList();
将每个订单的数据分组为动态类型。
var orders = from p in rows
group p by new { OrderNumber = p.OrderNumber, CustomerID = p.CustomerID } into g
select new { OrderNumber = g.Key.OrderNumber, CustomerID = g.Key.CustomerID, OrderLines = g.ToList() };
创建XML文件(请注意输出xml将不符合您的确切要求,但此示例应该可以帮助您。)
var xEle = new XElement("Orders",
from order in orders
select new XElement("Order",
new XAttribute("OrderNumber", order.OrderNumber),
new XElement("CustomerId", order.CustomerID)
, from line in order.OrderLines
select new XElement("Line", new XAttribute("ProductCode", line.ProductCode),
new XElement("OrderQty", line.OrderQty))));
xEle.Save("orders.xml");
Console.WriteLine("Converted to XML");