this is my text file
Id:1
Source:86876786
Destination:878979723
Date:1/16/2021 3:31:22 PM
Status: Failed
Network :Jio
I have to store everything into individual array.I have to display like this id Source Destination Date Status Network
If I was tasked with this I would perhaps have something to hold the data:
public class X {
public int Id { get; set; }
public long Source { get; set; }
public long Destination { get; set; }
public DateTime TheDate { get; set; }
public string Status { get; set; }
public string Network { get; set; }
}
And then something to parse it:
var lines = File.ReadAllLines(path);
var exes = new List<X>();
var current = new X();
for(int i = 0; i < lines.Length; i++){
{
var bits = lines[i].Split(":", 2);
if (bits[0] == "Id") current.Id = int.Parse(bits[1]);
else if (bits[0] == "Source") current.Source = long.Parse(bits[1]);
else if (bits[0] == "Destination") current.Destination = long.Parse(bits[1]);
else if (bits[0] == "Date") current.TheDate = DateTime.ParseExact(bits[1], "M/dd/yyyy h:mm:ss tt", CultureInfo.InvariantCulture);
else if (bits[0] == "Status") current.Status = bits[1].Trim();
else if (bits[0] == "Network ") current.Network = bits[1];
if(i % 6 == 5){
exes.Add(current);
current = new();
}
}
It will tolerate lines in any order, but they have to come in groups of 6. If they will come in the same order, but might not be in groups of 6 (eg if the Status line is unknown then it's missing from the file) we can use a hash set to track if we've already read a value for the current item
var exes = new List<X>();
var current = new X();
var hs = new HashSet<string>();
foreach (var line in lines)
{
var bits = line.Split(":", 2);
if (!hs.Add(bits[0]))
{
exes.Add(current);
current = new();
hs.Clear();
}
if (bits[0] == "Id") current.Id = int.Parse(bits[1]);
else if (bits[0] == "Source") current.Source = long.Parse(bits[1]);
else if (bits[0] == "Destination") current.Destination = long.Parse(bits[1]);
else if (bits[0] == "Date") current.TheDate = DateTime.ParseExact(bits[1], "M/dd/yyyy h:mm:ss tt", CultureInfo.InvariantCulture);
else if (bits[0] == "Status") current.Status = bits[1];
else if (bits[0] == "Network") current.Network = bits[1];
}
If they change order and don't come in groups of 6, you need something else to delimit groups. Use JSON
--
Then just go through your List<X>
and diplay them how you want. Give X a better name too
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.