private static string ApplyActionToLayoutXml(string xml, Func<RenderingDefinition, RenderingActionResult> action)
{
LayoutDefinition layout = LayoutDefinition.Parse(xml);
xml = layout.ToXml(); // normalize the output in case of any minor XML differences (spaces, etc)
// loop over devices in the rendering
for (int deviceIndex = layout.Devices.Count - 1; deviceIndex >= 0; deviceIndex--)
{
var device = layout.Devices[deviceIndex] as DeviceDefinition;
if (device == null) continue;
// loop over renderings within the device
for (int renderingIndex = device.Renderings.Count - 1; renderingIndex >= 0; renderingIndex--)
{
var rendering = device.Renderings[renderingIndex] as RenderingDefinition;
if (rendering == null) continue;
// run the action on the rendering
RenderingActionResult result = action(rendering);
// remove the rendering if the action method requested it
if (result == RenderingActionResult.Delete)
device.Renderings.RemoveAt(renderingIndex);
}
}
string layoutXml = layout.ToXml();
// save a modified layout value if necessary
if (layoutXml != xml)
{
return layoutXml;
}
return null;
}
}