I sense that what you're trying to do is replace some invalid characters while publishing. You would typically do this on the Package.Output
variable, not on the Component itself. Output is the text that gets published as part of the Component Presentation.
You typically don't want to modify the Component while publishing it. Before is fine, after is fine, but during is weird. Here is why: when you're doing this logic inside of a TBB, you are within a rendering/publishing session for the latest version of the Component. So any changes/saves you make to the Component within the publishing session, will create a new version of the Component, which will not be the version that's within your current session.
There is also a protective mechanism in the TOM.NET API that prevents saving Components within the publish session (probably for this reason). In the Tridion.ContentManager.config
the setting is allowWriteOperationsInTemplates
and it is off by default.
If you still really want to save a Component with your TBB using your approach. Place your updated Component XML back into comp.Content. Then call comp.CheckIn()
. Otherswise, use the Event System and do this on the Component Save event.
For reference also see: Is it possible to assign or change the value of a component Field during templating?