加载txt文件时遇到System.IndexOutOfRangeException

时间:2014-04-15 06:57:12

标签: c# text split indexing indexoutofboundsexception

我正在尝试加载一个以某种格式编写的txt文件,然后我遇到了System.IndexOutOfRangeException。您对我的代码有什么问题有任何疑问吗?谢谢!

txt.File:

P§Thomas§40899§2§§§
P§Damian§40726§1§§§

P =人;托马斯=名字; 40899 = ID; 2 =状态

这是我的代码:

using (StreamReader file = new StreamReader(fileName))
            {
                while (file.Peek() >= 0)
                {
                    string line = file.ReadLine();

                    char[] charSeparators = new char[] { '§' };
                    string[] parts = line.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries);

                    foreach (PersonId personids in PersonIdDetails)
                    {
                        personids.ChildrenVisualisation.Clear();

                        foreach (PersonId personidchildren in personids.Children)
                        {
                            personidchildren.FirstName = parts[1];
                            personidchildren.ID = parts[2];
                            personidchildren.Status = parts[3];

                            personids.ChildrenVisualisation.Add(personidchildren);
                        }
                    }                             
                }
            }

在部分[1]处抛出了异常。

4 个答案:

答案 0 :(得分:1)

您应该检查parts是否有足够的项目

  ...

  string[] parts = line.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries);

  foreach (PersonId personids in PersonIdDetails) {
    personids.ChildrenVisualisation.Clear();

    // Check if parts have enough infirmation: at least 3 items
    if (parts.Length > 3) // <- Pay attention for "> 3"
      foreach (PersonId personidchildren in personids.Children) {
        //TODO: Check, do you really start with 1, not with 0?
        personidchildren.FirstName = parts[1];
        personidchildren.ID = parts[2];
        personidchildren.Status = parts[3];

        personids.ChildrenVisualisation.Add(personidchildren);
      }
    else {
      // parts doesn't have enough data
      //TODO: clear personidchildren or throw an exception 
    } 
  }   

  ...

答案 1 :(得分:0)

将第一行更改为 使用(StreamReader file = new StreamReader(fileName,Encoding.GetEncoding(&#34; iso-8859-1&#34;));

答案 2 :(得分:0)

可能的方法,这只是更多解决方案之间的一个解决方案

    using (StreamReader file = new StreamReader(fileName))
        {
            while (file.Peek() >= 0)
            {
                string line = file.ReadLine();

                char[] charSeparators = new char[] { '§' };
                string[] parts = line.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries);

                foreach (PersonId personids in PersonIdDetails)
                {
                    personids.ChildrenVisualisation.Clear();

                    foreach (PersonId personidchildren in personids.Children)
                    {
                       if(parts.Length > 3)//Only if you want to save lines with all parts but you can create an else clause for other lines with 1 or 2 parts depending on the length
                       {
                        personidchildren.FirstName = parts[1];
                        personidchildren.ID = parts[2];
                        personidchildren.Status = parts[3];

                        personids.ChildrenVisualisation.Add(personidchildren);
                       }
                    }
                }                             
            }
        }

答案 3 :(得分:0)

我给人的印象是实际文件不是那么大,所以使用File.ReadAllLines代替(con是您需要将整个文件放在内存中)可能会很有用,这样就可以了所有的路线。

此外,可能需要删除空或只是空格的行。

        foreach (var line in File.ReadAllLines(fileName).Where(l => !string.IsNullOrWhiteSpace(l))
        {
           char[] charSeparators = new char[] { '§' };
           string[] parts = line.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries);

           foreach (PersonId personids in PersonIdDetails)
           {
             personids.ChildrenVisualisation.Clear();

             foreach (PersonId personidchildren in personids.Children)
             {
               personidchildren.FirstName = parts[1];
               personidchildren.ID = parts[2];
               personidchildren.Status = parts[3];
               personids.ChildrenVisualisation.Add(personidchildren);
             }
           }
         }