I ran into an interesting problem, got it to work, but the answer isn't making sense in my head.
The task is to delete the xData for a registered app on a given entity. You can see I basically copy all the existing xData into a new ResultBuffer, and leave out the TypedValues from the 'offending' registered app. Well it wasn't working. I'd step through the code, and see the updated ResultBuffer written to the object, with no error thrown. I'd check the entity with dxflist, and the 'offending' xData was still there. Looking at
this post, I noticed that that Virupaksha leaves the 1001 typed value in the xdata, so I tried that (line 27) and it seems to work.
/// <summary>
/// Deletes xdata under the regsitered app specified.
/// </summary>
/// <param name="objID">The object identifier.</param>
/// <param name="appName">name of the registered app to delete</param>
public static void DeleteXData(ObjectId objID, string appName)
{
if (appName.Length<1) return;
ResultBuffer updatedXdata
= new ResultBuffer
(); using (LockedTransaction tr = Active.Document.TransactionManager.StartLockedTransaction())
{
// remove any xdata for the given appname
DBObject obj = tr.GetObject(objID, OpenMode.ForWrite);
ResultBuffer existingData = obj.XData;
if (existingData!=null)
{
bool included = true;
// Get all values NOT with our Registered App
foreach (TypedValue tv in existingData)
{
// check whether to keep it
if (tv.TypeCode == CODE_APPNAME)
{
if (appName.Equals(tv.Value.ToString()))
{
// add the appname TV or the xData doesn't get set correctly and reverts back to the old xData
updatedXdata.Add(tv);
included = false;
}
else
{
included = true;
}
}
if (included)
{
updatedXdata.Add(tv);
}
}
RegisterApps(updatedXdata);
obj.XData = updatedXdata;
}
tr.Commit();
}
}
After running this, the offending registered app is no longer included in the xdata. I'm confused why I had to include the registered app TV in the updated xdata, and was hoping someone here could either provide an explanation or point me something in the dev docs that explain this. Primarily for the sake of easing my mind that I (hopefully) am doing it right, and also to help others (maybe future me) down the road.