【问题标题】:Fetching the zip code value from XML & match the zipcode of CSV file & display the corresponding sort code of csv to .zpl file从 XML 中获取邮政编码值并匹配 CSV 文件的邮政编码并将 csv 的相应排序代码显示到 .zpl 文件
【发布时间】:2017-06-13 15:59:33
【问题描述】:

我有一个 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;}

我需要一个代码

【问题讨论】:

    标签: c# xml csv


    【解决方案1】:

    在下面使用我的 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;
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      你可以做的几种方法。

      自己编写 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];
      

      披露:我是这个库的作者

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-04-21
        • 2011-04-11
        • 1970-01-01
        • 1970-01-01
        • 2013-04-05
        • 2013-02-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多