DICOM tag identifiers may arrive in different forms: a keyword such as PatientName in a configuration file, or a hexadecimal identifier such as 00100010 in a user’s input. Resolving either form to a Tag object lets your C# application work with the attribute identifier before using it in a dataset operation.
In this tutorial, you’ll use Aspose.Medical for .NET and Tag.TryParse to parse DICOM keywords and hexadecimal identifiers. You’ll also look up a keyword with TagDictionary.Default.FindByKeyword and check each result before using it. These examples resolve tag identifiers; they do not open a DICOM file or read patient values.
Install Aspose.Medical for .NET
Add Aspose.Medical for .NET to your project through Visual Studio’s Package Manager Console:
Install-Package Aspose.Medical
Alternatively, run this command from your project directory:
dotnet add package Aspose.Medical
Import the namespace used by all three examples:
using Aspose.Medical.Dicom.Tags;
The snippets use nullable reference syntax (Tag?). Use a C# project that supports this syntax and enable nullable reference types if you want nullable analysis. Check the target-framework requirements for your installed package version.
No input DICOM file or DICOMweb endpoint is needed for these identifier lookups.
Parse a DICOM Tag from a Keyword in C#
A DICOM keyword identifies an attribute using its standard name. For example, PatientName identifies the patient-name attribute.
Pass the keyword to Tag.TryParse and check the Boolean return value:
// Parse a tag from a DICOM keyword
if (Tag.TryParse("PatientName", out Tag? patientNameTag))
{
System.Console.WriteLine($"Tag: {patientNameTag}");
}
When parsing succeeds, the output parameter contains the resolved Tag, and the program prints its representation. In this example, PatientName corresponds to tag (0010,0010).
The result identifies the attribute. It does not contain a patient’s name. To obtain that value, you would need to read the corresponding element from a DICOM dataset.
Parse an Eight-Character Hexadecimal Tag Identifier
A DICOM tag has a group number and an element number. For (0010,0010), the first 0010 is the group and the second is the element. The eight-character string 00100010 combines them without a separator.
Use Tag.TryParse to resolve that hexadecimal identifier:
// Parse a tag from an eight-character hexadecimal identifier
if (Tag.TryParse("00100010", out Tag? hexTag))
{
System.Console.WriteLine($"Tag: {hexTag}");
}
This input resolves to the same tag as PatientName. Use this approach when your application receives tag identifiers in the compact hexadecimal form shown here.
Check the return value before using the output parameter. If parsing fails, handle the unsuccessful result in your calling code instead of continuing as though a tag had been resolved. The snippet prints a result only when parsing succeeds; it does not display an error message for unsuccessful input.
Look Up a DICOM Tag with TagDictionary
When your input is a keyword, you can also use TagDictionary.Default.FindByKeyword. The following example looks up StudyDate and checks the nullable result:
// Look up a tag by keyword in the dictionary
Tag? found = TagDictionary.Default.FindByKeyword("StudyDate");
if (found != null)
{
System.Console.WriteLine($"Study Date tag: {found}");
}
The program prints the resolved tag only when found is not null. The result identifies the study-date attribute; it does not return the date stored in a particular study.
Choose the method according to the kind of input your application accepts:
| Input | Method | Result check |
|---|---|---|
| Keyword or eight-character hexadecimal identifier | Tag.TryParse | Check the Boolean return value. |
| Keyword for a dictionary lookup | TagDictionary.Default.FindByKeyword | Check the result for null. |
Run the Examples and Check the Results
To run the snippets in a console project, place the namespace import at the top of Program.cs, then add the three lookup examples below it. Their variable names are different, so they can appear in the same scope.
Run the project from its directory:
dotnet run
The two Tag.TryParse examples print lines beginning with Tag:. Both resolve the patient-name identifier. The dictionary example prints a line beginning with Study Date tag: when its lookup succeeds.
| Input | Expected resolved attribute |
|---|---|
PatientName | Patient Name, (0010,0010) |
00100010 | Patient Name, (0010,0010) |
StudyDate | Study Date |
The console statements use string interpolation to display the returned tag objects. The table describes the expected lookup results rather than prescribing the exact formatting of that text.
Troubleshoot DICOM Tag Parsing
Check the keyword or hexadecimal input
Start with the exact inputs shown in the examples. For the hexadecimal example, use the eight-character string 00100010. Do not assume that additional punctuation, whitespace, alternative spellings, or different casing are accepted without checking the behavior of your installed version.
Handle unsuccessful results explicitly
The if statements in the snippets intentionally print only successful results. If your program produces no output, check whether TryParse returned false or FindByKeyword returned null. Add an appropriate message or validation response for the user of your application.
Keep identifiers separate from dataset values
A resolved PatientName tag identifies where a patient-name value belongs in a dataset. It does not establish that a particular file contains that attribute or that its value is populated. Keep input parsing and dataset-value access as separate steps in your application.
Check package and namespace references
If Tag or TagDictionary cannot be resolved, confirm that the project references Aspose.Medical and includes using Aspose.Medical.Dicom.Tags;. If another imported namespace also defines Tag, use the fully qualified type name Aspose.Medical.Dicom.Tags.Tag to distinguish it.
Conclusion
Use Tag.TryParse when a tag identifier may be supplied as a keyword or hexadecimal string. For a keyword-only dictionary lookup, use TagDictionary.Default.FindByKeyword. Check the result before passing the resolved tag into your dataset-processing code.
For related APIs, see the Aspose.Medical documentation and API reference. If you need a license for evaluation, you can request a temporary license.
Frequently Asked Questions
Can Tag.TryParse handle both keywords and hexadecimal IDs?
Yes. The examples parse PatientName and the eight-character hexadecimal identifier 00100010 into the corresponding Tag objects. Both inputs identify the same attribute.
What namespace must I import to use Tag and TagDictionary?
Add using Aspose.Medical.Dicom.Tags; to your C# file.
Does parsing PatientName retrieve a patient’s name?
No. It resolves the attribute identifier. Reading its value requires a dataset containing that attribute and a separate value-access operation.
What should I do when TryParse returns false?
Handle the unsuccessful result before continuing. For example, ask for corrected input or return a validation message. Do not use the output as a successfully resolved tag.
When should I use FindByKeyword instead of TryParse?
Use FindByKeyword for dictionary-based keyword lookup. Use TryParse when your input may be either a keyword or a hexadecimal identifier.
