Hi @Ansh , Welcome to Microsoft Q&A,
Make sure the byte offsets you are reading the file from are correct, especially after parsing line 12, and make sure byteCounter is in the correct state on each iteration. If there is a problem with the offset calculation, it may cause parsing errors.
When PropertyCountInt exceeds the expected value, you skip the line, which may be caused by improper handling of the value. You can try to debug the PropertyCountInt after each line of parsing to make sure it is correct.
Check if the code below helps. If there is an error, please comment the information below and I will follow up.
public void ParseFile()
{
try
{
RowList.Clear();
MetaDataBegin = Contents.Take(12).ToArray();
NumberOfRows = Contents.Skip(12).Take(4).ToArray();
NumberOfRowsInt = BitConverter.ToInt32(NumberOfRows, 0);
int byteCounter = 16;
Console.WriteLine($"NUMBER OF ROWS: {NumberOfRowsInt}");
for (int i = 1; i <= NumberOfRowsInt; i++)
{
Console.WriteLine($"PARSING ROW: {i}");
var row = new Row
{
StartIndex = byteCounter,
PropertyList = new List<Property>()
};
row.PropertyCount = Contents.Skip(byteCounter).Take(4).ToArray();
byteCounter += 4;
row.PropertyCountInt = BitConverter.ToInt32(row.PropertyCount, 0);
Console.WriteLine($"Row {i} PropertyCount: {row.PropertyCountInt}");
//
if (row.PropertyCountInt < 0 || row.PropertyCountInt > 100)
{
Console.WriteLine($"Warning: Unexpected PropertyCountInt {row.PropertyCountInt} at Row {i}. Skipping.");
continue;
}
for (int p = 1; p <= row.PropertyCountInt; p++)
{
var property = new Property
{
Tag = Contents.Skip(byteCounter).Take(4).ToArray()
};
byteCounter += 4;
property.TagBinary = ConvertByte2Hex(property.Tag);
property.Reserved = Contents.Skip(byteCounter).Take(4).ToArray();
byteCounter += 4;
property.Union = Contents.Skip(byteCounter).Take(8).ToArray();
byteCounter += 8;
if (property.TagBinary.StartsWith("1F") || property.TagBinary.StartsWith("02"))
{
property.Bytes = Contents.Skip(byteCounter).Take(4).ToArray();
byteCounter += 4;
property.BytesInt = BitConverter.ToInt32(property.Bytes, 0);
if (property.BytesInt < 0 || property.BytesInt > Contents.Length)
{
Console.WriteLine($"Warning: Unexpected BytesInt {property.BytesInt} at Row {i}, Property {p}. Skipping.");
break;
}
property.Value = Contents.Skip(byteCounter).Take(property.BytesInt).ToArray();
byteCounter += property.BytesInt;
if (property.TagBinary.StartsWith("1F"))
{
var stringBuilder = new StringBuilder();
foreach (var b in property.Value)
{
if (b != 0) // Skip null characters
{
stringBuilder.Append((char)b);
}
}
property.ValueStr = stringBuilder.ToString();
//
if (property.TagBinary == "1F000160")
{
row.Email = property.ValueStr;
}
}
}
row.PropertyList.Add(property);
}
RowList.Add(row);
}
MetaDataEnd = Contents.Skip(byteCounter).Take(12).ToArray();
}
catch (Exception ex)
{
Console.WriteLine($"Error during file parsing: {ex.Message}");
this.Exception = ex;
}
}
Best Regards,
Jiale
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.