AI OnAI Off
I found the solution. What you need to do is:
So in my case, the first fragment was static HTML and had a typo in it. We needed to find this and fix it, so the pseudo-algorithm looked like this:
var clone = myBlock.CreateWritableClone () as MyBlock;
if (clone != null && clone.MainBody != null && clone.MainBody.Fragments.Any ()) {
// replace first fragment with new bit of HTML:
var targetVal = "<h1>Hello World</h1>";
var newXhtmlStringValue = new XhtmlString ();
newXhtmlStringValue.Fragments.Add (new StaticFragment (targetVal));;
// skip first fragment and add the rest to the new xhtml string
clone.MainBody.Fragments.RemoveAt (0); // purge first item from old body
foreach (var fragment in clone.MainBody.Fragments) {
newXhtmlStringValue.Fragments.Add (fragment);
}
// set the main body over to the new xhtml string
clone.MainBody = newXhtmlStringValue;
// save
_contentRepository.Save ((IContent) clone, SaveAction.Publish, AccessLevel.NoAccess);
}
My initial suggestion would be something like this:
var clone = myBlock.CreateWritableClone () as MyBlock;
if (clone != null && clone.MainBody != null)
{
var oldText = clone.MainBody.ToInternalString();
var newText = oldtext.Replace("old", "new");
clone.MainBody = new XhtmlString(newText);
_contentRepository.Save ((IContent) clone, SaveAction.Publish, AccessLevel.NoAccess);
}
But your approach looks safer. Maybe I will mess up blocks, personalization or some other stuff I did not think of?
Correct Tomas, converting the entire XhtmlString to an internal string, and then parsing it back will break any dynamic fragments inside (personalized, blocks, etc). This could possibly be a bug on the XhtmlString constructor. I couldn't find any other way around this except to copy the fragments out one by one and inject a new static fragment in the target location.
Hi,
I have a situation where I need to update a bit of text in an XhtmlString programatically. I want to replace an old value with a new value. The value to be replaced is also the first "fragment" in the XhtmlString.
Currently the XhtmlString is composed of multiple fragments- some are simple HTML text, some are images, and some are additional blocks added into the XhtmlString. I am trying to update just the first fragment and set it to a new bit of HTML.
Here's what I'm trying:
When I run this, however, I seem to lose the blocks that have been added to the rich text.
What is the proper way to go about replacing 1 fragment of an XhtmlString with another?