从XML&获取邮政编码值匹配CSV文件的邮政编码&显示csv到.zpl文件的相应排序代码

时间:2017-06-13 15:59:33

标签: c# xml csv

我有一个xml文件,我从中提取了zipcode的数据:

newShippingLabel.Consignee.Zip = shipment.Consignee.Address.PostalCode;

我有一个包含2列的CSV文件:

ZipCode Sort Code
49801   12
49802   12
49858   15
49870   12
49876   12
49938   13
50001   20

等......

如果Consignee.Zip = 49801那么我需要从csv中获取该邮政编码的排序代码值,即49801 - >作为12&将它存储在SortCode变量中。 我已定义:

public string SortCode { get; set;}

我需要一个代码

2 个答案:

答案 0 :(得分:0)

使用下面的CSVReader。它会将CSV读入数据表,这样可以很容易地使用linq查找邮政编码。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.OleDb;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        const string FILENAME = @"c:\temp\test.csv";
        private void button1_Click(object sender, EventArgs e)
        {
            CSVReader csvReader = new CSVReader();
            DataSet ds = csvReader.ReadCSVFile(FILENAME, true);
            dataGridView1.DataSource = ds.Tables["Table1"];
        }
    }
    public class CSVReader
    {

        public DataSet ReadCSVFile(string fullPath, bool headerRow)
        {

            string path = fullPath.Substring(0, fullPath.LastIndexOf("\\") + 1);
            string filename = fullPath.Substring(fullPath.LastIndexOf("\\") + 1);
            DataSet ds = new DataSet();

            try
            {
                if (File.Exists(fullPath))
                {
                    string ConStr = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}" + ";Extended Properties=\"Text;HDR={1};FMT=Delimited\\\"", path, headerRow ? "Yes" : "No");
                    string SQL = string.Format("SELECT * FROM {0}", filename);
                    OleDbDataAdapter adapter = new OleDbDataAdapter(SQL, ConStr);
                    adapter.Fill(ds, "TextFile");
                    ds.Tables[0].TableName = "Table1";
                }
                foreach (DataColumn col in ds.Tables["Table1"].Columns)
                {
                    col.ColumnName = col.ColumnName.Replace(" ", "_");
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return ds;
        }
    }
}

答案 1 :(得分:0)

你可以采取多种方式。

自己编写CSV解析器,将zipcode - sortcode数据缓存到字典中

var zipSortCodeDict = File.ReadAllLines("zipCodes.csv").ToDictionary(line => line.Split("  ")[0], line => line.Split("  ")[1]);

然后轻松访问它们,如下所示

//get the sort code
string zipCode = "49876";
string sortCode = zipSortCodeDict[zipCode];

如果CSV文件变得复杂,您可以使用外部库加载CSV文件并将其缓存为字典。然后,您可以轻松访问排序代码。

这是一个可以通过CSV解析轻松完成的库,Cinchoo ETL - 一个开源框架

var zipSortCodeDict = new ChoCSVReader("zipCodes.csv").WithDelimiter("   ").WithFirstLineHeader().ToDictionary(kvp => kvp.ZipCode, kvp => kvp.SortCode);

//get the sort code
string zipCode = "49876";
string sortCode = zipSortCodeDict[zipCode];

披露:我是这个图书馆的作者

相关问题