DicomDataSet ds = new DicomDataSet("Dataset.dcm");
ds.ForEachAttribute(path =>
{
int group = path.TargetAttribute.Group;
int element = path.TargetAttribute.Element;
string tagFormatted = $"({group:X4},{element:X4})";
string keyword = path.TargetAttribute.Keyword;
object value = path.TargetAttribute.Value;
string displayValue;
if (value == null)
{
displayValue = "(null)";
}
else if (value is string s)
{
displayValue = s;
}
else if (value is Array arr && !(value is byte[]))
{
var firstItems = arr.Cast<object>().Take(5).Select(v => v?.ToString()?.Trim() ?? "(null)");
displayValue = string.Join(", ", firstItems);
if (arr.Length > 5) displayValue += " ...";
}
else if (value is System.UInt16[,,])
{
displayValue = "(Pixel Data: UInt16[,,] - not displayed)";
}
else
{
displayValue = value.ToString()?.Trim();
}
Console.WriteLine($"{tagFormatted} {keyword} = {displayValue}");
});
// Output:
(0008,0008) ImageType = ORIGINAL, PRIMARY, AXIAL, VOLUME
(0008,0012) InstanceCreationDate = 14/01/1997 00:00:00
(0008,0013) InstanceCreationTime = 01/01/0001 03:50:56
.....