Unless you have a schema or some other set of code dealing with it, the parsed document does not know that the ID attribute you want it to know about is in fact an ID. Setting it while signing and before you lose the DOM will work, but parsing it again from disk is not going to be done in a way that gets that ID established, so you'll get an error as it tries to locate the proper element in the DOM.
That may not be right, but for a quick eyeball over that much code, that would be my guess. -- Scott