DicomObjects.NET.8.48 Documentation
DicomObjects Namespace / DicomDataSet Class / ForEachAttribute Method
Delegate, which takes as a parameter, the path to each attribute.
Example

In This Topic
    ForEachAttribute Method
    In This Topic
    Recursively perform an action on every attribute within the dataset
    Syntax
    'Declaration
     
    
    Public Sub ForEachAttribute( _
       ByVal action As System.Action(Of SequencePath) _
    ) 
    public void ForEachAttribute( 
       System.Action<SequencePath> action
    )

    Parameters

    action
    Delegate, which takes as a parameter, the path to each attribute.
    Example
    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
    .....
    Requirements

    Target Platforms: .NET CLR 4.8 or higher

    See Also