Leadtools.Dicom.Common Send comments on this topic. | Back to Introduction - All Topics | Help Version 17.0.3.29
SaveXmlCallback Delegate
See Also  Example
Leadtools.Dicom.Common.Extensions Namespace : SaveXmlCallback Delegate



data
An instance of the SaveXmlData interface containing the progress callback data.
Optional delegate method that is used with the SaveXml methods.

Syntax

Visual Basic (Declaration) 
Public Delegate Function SaveXmlCallback( _
   ByVal data As SaveXmlData _
) As Boolean
Visual Basic (Usage)Copy Code
Dim instance As New SaveXmlCallback(AddressOf HandlerMethod)
C# 
public delegate bool SaveXmlCallback( 
   SaveXmlData data
)
C++/CLI 
public delegate bool SaveXmlCallback( 
   SaveXmlData^ data
)

Parameters

data
An instance of the SaveXmlData interface containing the progress callback data.

Return Value

return true to write this DICOM element in the output xml file; return false to skip writing this element in the output xml file

Example

This example loads a sample DICOM file, and saves the contents as an xml file. The xml file output is customized by using the SaveXmlCallback delegate.

  • 'element' is changed to 'element_newname'
  • 'tag' attribute is changed to 'tag_newname'
  • add an attribute to the 'dataset' element: 'new_attribute'
  • a comment is added: 'NEW COMMENT: customized xml file using the SaveXmlCallback'
Then the customized xml file is read into a DicomDataSet object using the LoadXmlCallback

Visual BasicCopy Code
Public Sub SaveXmlCallbackExample()
    Dim dicomFileNameIn As String = LeadtoolsExamples.Common.ImagesPath.Path & "IMAGE3.dcm"
    Dim xmlFileNameOut As String = LeadtoolsExamples.Common.ImagesPath.Path & "test.xml"
    Dim dicomFileNameOut As String = LeadtoolsExamples.Common.ImagesPath.Path & "test.dcm"

    ' Initialize DICOM engine
    DicomEngine.Startup()

    Dim ds As DicomDataSet = New DicomDataSet()

    ' Load an existing DICOM file
    ds.Load(dicomFileNameIn, DicomDataSetLoadFlags.None)

    ' Save as XML to a file with no binary data
    ' For the demo, keep the xml output file size small by skipping the pixel data
    ' Use the SaveXmlCallback delegate to customize the xml file
    Dim xmlFlags As DicomDataSetSaveXmlFlags = DicomDataSetSaveXmlFlags.IgnoreBinaryData Or DicomDataSetSaveXmlFlags.TrimWhiteSpace Or DicomDataSetSaveXmlFlags.TagWithCommas

    ds.SaveXml(xmlFileNameOut, xmlFlags, New SaveXmlCallback(AddressOf MySaveXmlCallback))

    ' Use a LoadmlCallback to read the customized xml file
    ds.LoadXml(xmlFileNameOut, DicomDataSetLoadXmlFlags.None, New LoadXmlBeforeElementCallback(AddressOf MyLoadXmlBeforeElementCallback), New LoadXmlAfterElementCallback(AddressOf MyLoadXmlAfterElementCallback))

    ' Save the result -- there will be no pixel data
    ds.Save(dicomFileNameOut, DicomDataSetSaveFlags.None)

    DicomEngine.Shutdown()
End Sub

Private Function MySaveXmlCallback(ByVal data As SaveXmlData) As Boolean
    'string sTag = data.attributes["tag"];
    'if (sTag.StartsWith("0008"))
    '   return false;
    If data.NodeType = DicomXmlNodeType.Comment Then
        data.Comment = "COMMENT: customized xml file using the SaveXmlCallback"
        Return False
    End If

    If data.ElementName = "dataset" Then
        data.Attributes.Add("new_attribute", "some_value")
    ElseIf data.ElementName = "element" Then
        data.ElementName = "element_newname"
        Dim sTagValue As String = data.Attributes("tag")
        data.Attributes.Remove("tag")
        data.Attributes.Add("tag_newname", sTagValue)
    End If
    Return True
End Function

Private Function MyLoadXmlBeforeElementCallback(ByVal data As LoadXmlBeforeElementData) As Boolean
    Dim sTagValue As String = data.Attributes("tag_newname")
    Dim tag As Long = 0
    sTagValue = sTagValue.Replace(",", String.Empty)
    If Long.TryParse(sTagValue, System.Globalization.NumberStyles.HexNumber, Nothing, tag) Then
        data.Tag = tag
    End If
    Return True
End Function

Private Sub MyLoadXmlAfterElementCallback(ByVal data As LoadXmlAfterElementData)
    If data.DicomElement.Tag = DicomTag.PixelData Then
        ' here you could call one of the following to set the pixel data
        '    data.DicomDataSet.SetBinaryValue 
        '    data.DicomDataSet.SetImage()
    End If
End Sub
C#Copy Code
public void SaveXmlCallbackExample()
{
   string dicomFileNameIn = LeadtoolsExamples.Common.ImagesPath.Path + "IMAGE3.dcm";
   string xmlFileNameOut = LeadtoolsExamples.Common.ImagesPath.Path + "test.xml";
   string dicomFileNameOut = LeadtoolsExamples.Common.ImagesPath.Path + "test.dcm";

   // Initialize DICOM engine
   DicomEngine.Startup();

   DicomDataSet ds = new DicomDataSet();

   // Load an existing DICOM file
   ds.Load(dicomFileNameIn, DicomDataSetLoadFlags.None);

   // Save as XML to a file with no binary data
   // For the demo, keep the xml output file size small by skipping the pixel data
   // Use the SaveXmlCallback delegate to customize the xml file
   DicomDataSetSaveXmlFlags xmlFlags =
      DicomDataSetSaveXmlFlags.IgnoreBinaryData |
      DicomDataSetSaveXmlFlags.TrimWhiteSpace |
      DicomDataSetSaveXmlFlags.TagWithCommas;

   ds.SaveXml(xmlFileNameOut, xmlFlags, new SaveXmlCallback(MySaveXmlCallback));

   // Use a LoadmlCallback to read the customized xml file
   ds.LoadXml(xmlFileNameOut, DicomDataSetLoadXmlFlags.None, new LoadXmlBeforeElementCallback(MyLoadXmlBeforeElementCallback), new LoadXmlAfterElementCallback(MyLoadXmlAfterElementCallback));

   // Save the result -- there will be no pixel data
   ds.Save(dicomFileNameOut, DicomDataSetSaveFlags.None);

   DicomEngine.Shutdown();
}

private bool MySaveXmlCallback(SaveXmlData data)
{
   //string sTag = data.attributes["tag"];
   //if (sTag.StartsWith("0008"))
   //   return false;
   if (data.NodeType == DicomXmlNodeType.Comment)
   {
      data.Comment = "COMMENT: customized xml file using the SaveXmlCallback";
      return false;
   }

   if (data.ElementName == "dataset")
   {
      data.Attributes.Add("new_attribute", "some_value");
   }
   else if (data.ElementName == "element")
   {
      data.ElementName = "element_newname";
      string sTagValue = data.Attributes["tag"];
      data.Attributes.Remove("tag");
      data.Attributes.Add("tag_newname", sTagValue);
   }
   return true;
}

private bool MyLoadXmlBeforeElementCallback(LoadXmlBeforeElementData data)
{
   string sTagValue = data.Attributes["tag_newname"];
   long tag = 0;
   sTagValue = sTagValue.Replace(",", string.Empty);
   if (long.TryParse(sTagValue, System.Globalization.NumberStyles.HexNumber, null, out tag))
      data.Tag = tag;
   return true;
}

private void MyLoadXmlAfterElementCallback(LoadXmlAfterElementData data)
{
   if (data.DicomElement.Tag == DicomTag.PixelData)
   {
      // here you could call one of the following to set the pixel data
      //    data.DicomDataSet.SetBinaryValue 
      //    data.DicomDataSet.SetImage()
   }
}

Remarks

This delegate is called once for each DICOM element before it is written to the XML file.

Requirements

Target Platforms: Microsoft .NET Framework 2.0, Windows 2000, Windows XP, Windows Server 2003 family, Windows Server 2008 family, Windows Vista, Windows 7

See Also

Leadtools.Dicom.Common requires a Document or Medical toolkit license and unlock key. For more information, refer to: Imaging Pro/Document/Medical Features