【问题标题】:C# IndexOutOfRangeException on CSV-FileCSV 文件上的 C#IndexOutOfRangeException
【发布时间】:2013-07-05 12:32:18
【问题描述】:

我想读取一个 CSV 文件并从中写入一个 XML 文件。我了解如何拆分 csv 行的基本思想。但是我的代码给了我一个例外,我不明白。

首先是代码:

    static void Main(string[] args)
    {
       Console.WriteLine("Insert Filename please");
       string path = Console.ReadLine();

           string[] source = File.ReadAllLines(path);
           XElement output = new XElement("Consumer",
               from str in source
               let fields = str.Split(',')
               select new XElement("consumer",
                   new XElement(fields[0],
                   new XAttribute(fields[1], fields[2]),
                   new XAttribute(fields[3], fields[4])
                   )));

                Console.WriteLine(output);
                Console.ReadLine();
    }

}

} 这就是代码,当我调试它时,我收到一条失败消息,上面写着“OutOfRangeException”。我知道这种异常意味着什么,但我就是找不到解决方案。我将逗号之间的每个字符都解释为一个字段。因此,通常数组不应大于 CSV 文件中的字段数量。为什么我现在得到 OutOfBounds,对我来说很神秘。

非常感谢您的阅读和帮助:)

编辑:我使用的 CSV 文件已损坏,因此我没有得到任何有用的信息。我已修复并且代码有效,但只要 CSV 不包含空字段。弹出的异常是,“名称不能以字符''开头,十六进制值 0x20。我不知道如何处理这个问题。我尝试使用 Lumenworks 的 FastCSVReader 库,但没有得到解决方案。

【问题讨论】:

  • source 中的所有字符串是否都包含 36 个 , 字符(包括最后一行)?
  • 检查fields数组的计数。它有 37 个或更多元素吗?
  • 能看到源文件就好了
  • 检查文件底部没有空行,因为这在过去给我造成了问题
  • 更多信息。源文件是一个 CSV 文件,每行的第一列中写有单词和数字,用逗号分隔。例如:GREAL,Great Lakes Food Market,Howard Snyder,Marketing Manager, (503) 555-7555,2732 Baker Blvd., Eugene,OR,97403,USA, 12 Orchestra Terrace,Walla Walla,WA,99362,USA , Lazy K Kountry Store,John Steel,营销经理,现在我发现我的文件有一点不同。这些值用“,”分隔,逗号后跟一个空格。这有什么区别吗??

标签: c# csv linq-to-xml indexoutofboundsexception


【解决方案1】:

必须至少有一行少于 37 个字段。也许最后有一个空行?

您可以在继续之前尝试检查fields.Length,即:

XElement toXML = new XElement("Root",
from str in source
let fields = str.Split(',')
where fields.Length >= 37 // <==== Check #fields
select new XElement("Consumer",
            new XAttribute("name", fields[0]),
        new XElement(fields[3]),
            new XAttribute(fields[4], fields[5]),
            new XAttribute(fields[6], fields[7]),
            new XAttribute(fields[8], fields[9]),
            new XAttribute(fields[10], fields[11]),
            new XAttribute(fields[12], fields[13]),
            new XAttribute(fields[14], fields[15]),
            new XAttribute(fields[16], fields[17]),
            new XAttribute(fields[18], fields[19]),
            new XAttribute(fields[20], fields[21]),
        new XElement(fields[22]),
              new XAttribute(fields[24], fields[25]),
              new XAttribute(fields[26], fields[27]),
              new XAttribute(fields[28], fields[29]),
        new XElement(fields[30]),
              new XAttribute(fields[32], ""),
              new XAttribute(fields[33], fields[34]),
              new XAttribute(fields[35], fields[36])));

【讨论】:

  • 感谢您的回复,我根据您的建议更改了代码。现在异常不再发生。谢谢你,但输出只是
  • @Alika87 我怀疑你的文件格式不正确。您可以将整个文件粘贴到您的问题中吗?
  • 在我的问题下的评论中,我发布了一个示例,我的文件是什么样的。
猜你喜欢
  • 2012-06-11
  • 2012-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-10
  • 2017-08-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多