【问题标题】:Ignoring the header line of an array read from CSV file outputting in XML format忽略从以 XML 格式输出的 CSV 文件中读取的数组的标题行
【发布时间】:2019-11-07 14:08:59
【问题描述】:

我正在尝试读取但不输出附加的这个 csv 文件的标题行。换句话说,在处理数据行之前读取标题行并忽略它。

命名空间 csvtoXML { 课堂节目 {

    static void Main( string[] args)
    {




    string[] source = File.ReadAllLines("GBPAY_103188.csv");
    XElement GBPAY = new XElement("EmployeeElectionImport",
        from str in source
        let fields = str.Split(',')
                  select new XElement("ElectionImport",
                  new XElement("EmployeeElection",
                  new XElement("Employee",fields[0]),
                  new XElement("Election", "\n",
                  new XElement("EarnDeductType", fields[3]),
                  new XElement("EarnDeductType","\n",
                  new XElement("PlanBenefitCode", fields[1], "\n"),
                  new XElement("SingleFamilyCode", fields[2], "\n"),
                  new XElement("EERate", fields[3], "\n"),
                   new XElement("ERRate", fields[4], "\n"),
                   new XElement("AnnualBenefitRate", fields[5], "\n"),
                   new XElement("EnrollmentCode", fields[6], "\n")
                ))))) ; ;
            Console.WriteLine(GBPAY);
}}}

【问题讨论】:

  • 你可以使用source.Skip(1)
  • 批判性思维是关键。您正在使用 ReadAllLines 方法,该方法将读取所有行。有没有办法不阅读所有行,跳过第一行?或者,您可以在处理删除第一行之前修改数组,或者您正在使用 LINQ 查询来获取数组中的所有元素。从源中选择时有什么方法可以跳过某些元素?
  • 嗯,所以没有办法读取标题行但也忽略同时输出它们? @杰里米
  • 我认为你错过了我的观点。所有问题的答案都是肯定的,有办法。有一个 ReadLines 方法可以让你按照你想要的方式构建你的数组。有一种方法可以在 LINQ 中跳过选择内容。

标签: c# arrays xml csv


【解决方案1】:

您应该: 必须一次读取一行,并且只输出您想要的行。 可以在here 中找到有关如何执行此操作的示例

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-19
    • 1970-01-01
    • 2014-05-08
    相关资源
    最近更新 更多