You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The XRecord class is a custom data storage object that can be stored in dictionaries (such as the Named Objects Dictionary or extension dictionaries). Unlike XData which is limited to ~16KB and attached directly to entities, XRecords can store unlimited amounts of structured data and are stored in dictionaries as named entries.
Namespace
Autodesk.AutoCAD.DatabaseServices
Inheritance Hierarchy
System.Object
└─ RXObject
└─ DBObject
└─ XRecord
Key Properties
Property
Type
Description
Data
ResultBuffer
Gets/sets the data stored in the XRecord
IsClonable
bool
Gets whether the XRecord can be cloned
MergeStyle
DictionaryMergeStyle
Gets/sets how the XRecord is merged during WBLOCK operations
Key Methods
Method
Return Type
Description
XRecord()
Constructor
Creates a new empty XRecord
Dispose()
void
Disposes the XRecord and releases resources
Code Examples
Example 1: Creating an XRecord in Named Objects Dictionary
using(Transactiontr=db.TransactionManager.StartTransaction()){// Get the Named Objects Dictionary (NOD)DBDictionarynod=tr.GetObject(db.NamedObjectsDictionaryId,OpenMode.ForWrite)asDBDictionary;// Create a custom dictionary if it doesn't existstringdictName="MyAppData";DBDictionarycustomDict;if(nod.Contains(dictName)){customDict=tr.GetObject(nod.GetAt(dictName),OpenMode.ForWrite)asDBDictionary;}else{customDict=newDBDictionary();nod.SetAt(dictName,customDict);tr.AddNewlyCreatedDBObject(customDict,true);}// Create an XRecord with dataXRecordxRec=newXRecord();ResultBufferrb=newResultBuffer(newTypedValue((int)DxfCode.Text,"Project Name"),newTypedValue((int)DxfCode.Int32,12345),newTypedValue((int)DxfCode.Real,3.14159));xRec.Data=rb;// Add XRecord to custom dictionarycustomDict.SetAt("ProjectInfo",xRec);tr.AddNewlyCreatedDBObject(xRec,true);rb.Dispose();tr.Commit();}
using(Transactiontr=db.TransactionManager.StartTransaction()){DBDictionarynod=tr.GetObject(db.NamedObjectsDictionaryId,OpenMode.ForWrite)asDBDictionary;// Create XRecord with structured dataXRecordxRec=newXRecord();ResultBufferrb=newResultBuffer(// HeadernewTypedValue((int)DxfCode.Text,"HEADER"),newTypedValue((int)DxfCode.Text,"Version"),newTypedValue((int)DxfCode.Text,"1.0"),// Project infonewTypedValue((int)DxfCode.Text,"PROJECT"),newTypedValue((int)DxfCode.Text,"Name"),newTypedValue((int)DxfCode.Text,"Building A"),newTypedValue((int)DxfCode.Text,"Date"),newTypedValue((int)DxfCode.Text,DateTime.Now.ToString("yyyy-MM-dd")),// Numeric datanewTypedValue((int)DxfCode.Text,"METRICS"),newTypedValue((int)DxfCode.Real,1234.56),newTypedValue((int)DxfCode.Real,7890.12),// 3D PointnewTypedValue((int)DxfCode.Point3d,newPoint3d(100,200,0)));xRec.Data=rb;if(!nod.Contains("AppSettings")){DBDictionaryappDict=newDBDictionary();nod.SetAt("AppSettings",appDict);tr.AddNewlyCreatedDBObject(appDict,true);appDict.SetAt("Config",xRec);tr.AddNewlyCreatedDBObject(xRec,true);}rb.Dispose();tr.Commit();}
Example 4: Attaching XRecord to Entity Extension Dictionary
using(Transactiontr=db.TransactionManager.StartTransaction()){// Get an entity (e.g., a line)Entityent=tr.GetObject(entityId,OpenMode.ForWrite)asEntity;// Create or get the entity's extension dictionaryif(ent.ExtensionDictionary==ObjectId.Null){ent.CreateExtensionDictionary();}DBDictionaryextDict=tr.GetObject(ent.ExtensionDictionary,OpenMode.ForWrite)asDBDictionary;// Create XRecord with custom dataXRecordxRec=newXRecord();ResultBufferrb=newResultBuffer(newTypedValue((int)DxfCode.Text,"CustomProperty"),newTypedValue((int)DxfCode.Text,"CustomValue"),newTypedValue((int)DxfCode.Int32,42));xRec.Data=rb;// Add to extension dictionarystringkey="MyAppData";if(extDict.Contains(key)){extDict.Remove(key);}extDict.SetAt(key,xRec);tr.AddNewlyCreatedDBObject(xRec,true);rb.Dispose();ed.WriteMessage($"\nAttached XRecord to entity {ent.GetType().Name}");tr.Commit();}
Example 5: Updating XRecord Data
using(Transactiontr=db.TransactionManager.StartTransaction()){DBDictionarynod=tr.GetObject(db.NamedObjectsDictionaryId,OpenMode.ForRead)asDBDictionary;if(nod.Contains("MyAppData")){DBDictionarycustomDict=tr.GetObject(nod.GetAt("MyAppData"),OpenMode.ForRead)asDBDictionary;if(customDict.Contains("ProjectInfo")){XRecordxRec=tr.GetObject(customDict.GetAt("ProjectInfo"),OpenMode.ForWrite)asXRecord;// Update the dataResultBuffernewRb=newResultBuffer(newTypedValue((int)DxfCode.Text,"Updated Project Name"),newTypedValue((int)DxfCode.Int32,99999),newTypedValue((int)DxfCode.Real,2.71828),newTypedValue((int)DxfCode.Text,"New Field"));xRec.Data=newRb;newRb.Dispose();ed.WriteMessage("\nXRecord data updated successfully");}}tr.Commit();}
Example 6: Deleting an XRecord
using(Transactiontr=db.TransactionManager.StartTransaction()){DBDictionarynod=tr.GetObject(db.NamedObjectsDictionaryId,OpenMode.ForRead)asDBDictionary;if(nod.Contains("MyAppData")){DBDictionarycustomDict=tr.GetObject(nod.GetAt("MyAppData"),OpenMode.ForWrite)asDBDictionary;if(customDict.Contains("ProjectInfo")){// Remove the XRecord from the dictionarycustomDict.Remove("ProjectInfo");ed.WriteMessage("\nXRecord removed successfully");}}tr.Commit();}
Example 7: Searching for XRecords in Named Objects Dictionary
using(Transactiontr=db.TransactionManager.StartTransaction()){DBDictionarynod=tr.GetObject(db.NamedObjectsDictionaryId,OpenMode.ForRead)asDBDictionary;ed.WriteMessage("\n=== Searching for XRecords in NOD ===");foreach(DBDictionaryEntryentryinnod){DBObjectobj=tr.GetObject(entry.Value,OpenMode.ForRead);if(objisXRecord){XRecordxRec=objasXRecord;ed.WriteMessage($"\nFound XRecord: {entry.Key}");ResultBufferrb=xRec.Data;if(rb!=null){ed.WriteMessage($" Data items: {rb.AsArray().Length}");rb.Dispose();}}elseif(objisDBDictionary){// Recursively search nested dictionariesDBDictionarysubDict=objasDBDictionary;ed.WriteMessage($"\nSearching dictionary: {entry.Key}");foreach(DBDictionaryEntrysubEntryinsubDict){DBObjectsubObj=tr.GetObject(subEntry.Value,OpenMode.ForRead);if(subObjisXRecord){ed.WriteMessage($" Found XRecord: {subEntry.Key}");}}}}tr.Commit();}
Example 8: Storing Binary Data in XRecord
using(Transactiontr=db.TransactionManager.StartTransaction()){DBDictionarynod=tr.GetObject(db.NamedObjectsDictionaryId,OpenMode.ForWrite)asDBDictionary;// Create XRecord with binary dataXRecordxRec=newXRecord();// Example: Store a serialized object as binary databyte[]binaryData=newbyte[]{0x01,0x02,0x03,0x04,0xFF,0xFE};ResultBufferrb=newResultBuffer(newTypedValue((int)DxfCode.Text,"BinaryDataRecord"),newTypedValue((int)DxfCode.BinaryChunk,binaryData),newTypedValue((int)DxfCode.Int32,binaryData.Length));xRec.Data=rb;if(!nod.Contains("BinaryData")){DBDictionarybinaryDict=newDBDictionary();nod.SetAt("BinaryData",binaryDict);tr.AddNewlyCreatedDBObject(binaryDict,true);binaryDict.SetAt("Data1",xRec);tr.AddNewlyCreatedDBObject(xRec,true);}rb.Dispose();ed.WriteMessage($"\nStored {binaryData.Length} bytes in XRecord");tr.Commit();}
Common DXF Codes for XRecord Data
Code
Type
Description
1
String
Text string
10
Point3d
3D point
40
Double
Floating-point value
70
Int16
16-bit integer
90
Int32
32-bit integer
280
Byte
8-bit integer
310
Binary
Binary data chunk
330
ObjectId
Soft-pointer ID/handle
340
ObjectId
Hard-pointer ID/handle
360
ObjectId
Hard-owner ID/handle
Best Practices
Use Dictionaries for Organization: Store XRecords in custom dictionaries within the Named Objects Dictionary for better organization
Dispose ResultBuffers: Always dispose ResultBuffer objects to prevent memory leaks
Use Meaningful Keys: Use descriptive dictionary keys for XRecords
Structure Your Data: Plan your data structure using consistent DXF codes
Extension Dictionaries: Use entity extension dictionaries to attach XRecords to specific entities
Transaction Management: Always use transactions when creating or modifying XRecords
Check for Existence: Check if dictionary entries exist before accessing them
Document Your Schema: Document the structure and DXF codes used in your XRecords
XRecord vs XData
Feature
XRecord
XData
Storage Location
Dictionaries (NOD, Extension Dict)
Attached directly to entities
Size Limit
Unlimited
~16KB per entity
Structure
Stored as named entries
Identified by application name
Organization
Hierarchical (nested dictionaries)
Flat (per entity)
Accessibility
Requires dictionary navigation
Direct entity property
Use Case
Large/complex data, shared data
Small entity-specific data
Performance
Slightly slower (dictionary lookup)
Faster (direct access)
When to Use XRecord
Large Data: When you need to store more than 16KB of data
Shared Data: When multiple entities need to reference the same data
Complex Structures: When you need hierarchical or nested data structures
Drawing-Level Data: When storing application settings or drawing-wide configuration
Entity Metadata: When attaching detailed metadata to specific entities via extension dictionaries