Modifying xml document in a custom pipeline component


I once came across a situation where I needed to check for empty fields and replace the empty values with a non-empty value to help in promotion of the those properties. Apparently empty fields with closing tags are not promoted by the XML disassembler (see this link). As a result, if you had an orchestration in which the messages were filtered based on certain promoted properties which turned out to be empty, the message would get suspended due to the xml disassembler not promoting these properties.

The solution to this was simple – to create a pipeline component which would replace empty fields with non-empty values.

Create a custom pipeline component which has the execute method implemented as per below and drop this component in the decode stage of your receive pipeline.

 public IBaseMessage Execute(IPipelineContext pContext, IBaseMessage pInMsg) { 
if (pInMsg == null || pInMsg.BodyPart == null || pInMsg.BodyPart.Data == null)
{
throw new ArgumentNullException("pInMsg");
}
var xmlDoc = new XmlDocument();
pInMsg.BodyPart.GetOriginalDataStream().Seek(0, SeekOrigin.Begin);
xmlDoc.Load(pInMsg.BodyPart.GetOriginalDataStream());
try
{
XmlNode statusNode = xmlDoc.SelectSingleNode("/*[local-name()='Applications' and
namespace-uri()='http://Company.Project.BT.Schemas.SchemaName_v1_0']/*[local-name()='Application'
and namespace-uri()='']/*[local-name()='StageFlag' and namespace-uri()='']");
if (statusNode != null)
{
Trace.WriteLine("Checking if StageFlag node is empty...");
if (string.IsNullOrEmpty(statusNode.InnerText))
{
statusNode.InnerText = "new value";
var ms = new MemoryStream();
xmlDoc.Save(ms);
ms.Seek(0, SeekOrigin.Begin);
pInMsg.BodyPart.Data = ms;
}
}
//this is required or else the next component will fail.
pInMsg.BodyPart.GetOriginalDataStream().Seek(0, SeekOrigin.Begin);
}
catch (Exception ex) { throw; }
return pInMsg;
}

Receive Pipeline

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: