Dear Sean, dear community,

I came across this article as I searched for a way to dynamically access the XData of a class. 

Many thanks, Sean, for letting me know. 
 

In exchange I may share my gained knowledge of dynamically adding a class and an XData block within it:

  // Create class
  set tNewClass = ##class(%Dictionary.ClassDefinition).%New("My.Package.Class")
  set tNewClass.ProcedureBlock = 1  // This is the default. If you don't set this, [Not ProcedureBlock] will be added to the class definition.
  //optional - but useful - class definition properties
  set tNewClass.Description = "This is a newly generated class in my package"
  set tNewClass.Super = "My.Package.SuperClass"
  set tSC = tNewClass.%Save()
  Return:$$$ISERR(tSC) tSC

  // Add XData definition to class
  set tDef = ##class(%Dictionary.XDataDefinition).%New("My.Package.Class")
  set tDef.Name = "MyNewXData"
  set tSC = tDef.%Save()

Now you can write your desired XML data to the tDef.Data stream.

Best regards,

Sebastian