I have a CSV file that contains data like the following below (but lots more):
Date dd/mm/yyyy
ExpirationDate dd/mm/yyyy
Lot 6760786776
Serial 34659FSFHS45
DataType Unknown Count
A(Loc1, Loc2) Unknown Variable1 Variable2 Variable3
B(Loc3, Loc4) Unknown Variable4 Variable5 Variable6
DataType Unknown Apple
A(Loc1, Loc2) Unknown Variable1 Variable2 Variable3
B(Loc3, Loc4) Unknown Variable4 Variable5 Variable6
etc....
Currently, I have something like this:
public void DeserialCSVStream(string filePath)
{
using (StreamReader sr = new StreamReader(filePath))
{
string currentline;
while ((currentline = sr.ReadLine()) != null)
{
if (currentline.IndexOf("Date", StringComparison.CurrentCultureIgnoreCase) >=0)
{
Console.WriteLine(currentline);
}
else if (currentline.IndexOf("Lot", StringComparison.CurrentCultureIgnoreCase) >= 0)
{
Console.WriteLine(currentline);
}
else if (currentline.IndexOf("Serial", StringComparison.CurrentCultureIgnoreCase) >= 0)
{
Console.WriteLine(currentline);
}
else if (currentline.IndexOf("Count", StringComparison.CurrentCultureIgnoreCase) >= 0)
{
Console.WriteLine(currentline);
}
}
}
}
Which is okay but gives me a few problems:
-If I look for a string of "Date", it gives me not only Date but Expiration Date but I only want to parse out the Date. If I use StartsWith, it gives me null.
-Also, the above only lets me grab the columns data next to the field. E.g. Count only returns DataType and Unknown but I want to grab the whole "table" under count and not just that line where Count is on. How do I do that?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…