Mình có vấn đề này xin các bạn bên công nghệ thông tin giúp cho. Mình tạo 1 Webservice đơn giản bằng ASP.NET. Mình co 1 file .txt. Nội dung như sau:
<Anh A> <1000>
<Chi B> <2000>
<Anh C> <3000>
<Anh D> <4000>
Mình muốn lấy tên và lương tháng của từng người và lưu vào 1 lớp gọi là Company.
public class Company
{
public string Name { get; set; }
public string Salary { get; set; }
}
Đây là code của mình viết trên VS2015:
[WebMethod]
public List<Company> CompanyGetInformation()
{
List<Company> company = new List<Company>();
//List<string> information = new List<string>();
string name = " ";
string salary= " ";
int startSubName, startSubSalary;
int endSubName, endSubSalary;
string path = "D:/Bi/Company.txt";
StreamReader reader = File.OpenText(path);
while(!reader.EndOfStream)
{
//information.Add(reader.ReadLine());
for (int i = 0; i <reader.ReadLine().Length;i++)
{
startSubName = reader.ReadLine().IndexOf("<");
startSubSalary = reader.ReadLine().LastIndexOf("<");
endSubName = reader.ReadLine().IndexOf(">");
endSubSalary = reader.ReadLine().LastIndexOf(">");
name = reader.ReadLine().Substring(startSubName + 1, endSubName);
salary = reader.ReadLine().Substring(startSubSalary + 1, endSubSalary);
}
company.Add(new Company() { Name = name, Salary = salary });
}
return company;
}
Khi mình chạy lên thì nó báo lỗi thế này:
System.ArgumentOutOfRangeException: Index and length must refer to a location within the string.
Parameter name: length
at System.String.Substring(Int32 startIndex, Int32 length)
at web.topicaservice.CompanyGetInformation() in D:\Bi\Bai 3 Web\web\web\topicaservice.asmx.cs:line 100
Các bạn cho mình hỏi giờ mình phải giải quyết như thế nào?