Tenalinaga's Blog

I am frog in a well. Every time I jump out of the well; I relialize I am in bigger well and eventually get bored which motivates me to get to next level.

Custom Pipeline Component To PreProcess XML (can be modified to add just AddNameSpace)

After Debatching there will times in which extracted nodes will not have name namespace. This will create problems in transformation shapes and message types not being matched. So this example shows custom component to preprocess xml nodes by adding namespace before it is split. Then you can use xml disassembler or ESBdispatcher disassembler components to split the envelope which will provide xsd schemas with namespaces.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.Collections;
using System.ComponentModel;
using System.Resources;
using System.Reflection;
//BizTalk Classes
using Microsoft.BizTalk.Message.Interop;
using Microsoft.BizTalk.Component.Interop;

namespace XXX.AddNameSpaceUpdateBatch
{
[ComponentCategory(CategoryTypes.CATID_PipelineComponent)]
[ComponentCategory(CategoryTypes.CATID_Any)]
[System.Runtime.InteropServices.Guid(“9d182f63-7663-4c42-b9e0-31088e92d369”)]
public class CustomAddNameSpaceUpdateBatch : Microsoft.BizTalk.Component.Interop.IPersistPropertyBag, Microsoft.BizTalk.Component.Interop.IBaseComponent, Microsoft.BizTalk.Component.Interop.IComponent, Microsoft.BizTalk.Component.Interop.IComponentUI
{

private string _NewNameSpace;
public string NewNameSpace
{
get { return _NewNameSpace; }
set { _NewNameSpace = value; }
}

#region IBaseComponent Members

public string Description
{
get
{
return (“Add NameSpace to XML”);
}
}

public string Name
{
get { return (“XmlUpdateRecordsStatusAddNameSpace”); }
}

public string Version
{
get { return (“1.0.0.0”); }
}

#endregion

#region IComponentUI Members

public IntPtr Icon
{
get { return new System.IntPtr(); }
}

public IEnumerator Validate(object projectSystem)
{
return null;
}

#endregion

#region IComponent Members
public IBaseMessage Execute(IPipelineContext pContext, IBaseMessage pInMsg)
{
IBaseMessagePart bodyPart = pInMsg.BodyPart;
//StringBuilder outputMessageText = null;
string messageType = “”;
if (bodyPart != null)
{
Stream originalStream = bodyPart.GetOriginalDataStream();
if (originalStream != null)
{
XmlDocument xdoc = new XmlDocument();
xdoc.Load(originalStream);

StringWriter sw = new StringWriter();
XmlTextWriter tx = new XmlTextWriter(sw);
xdoc.WriteTo(tx);
string _strXdoc = sw.ToString();
System.Diagnostics.EventLog.WriteEntry(“”, _strXdoc);
_strXdoc = _strXdoc.Replace(“

“, “”);
_strXdoc = _strXdoc.Replace(“
“, “”);
System.Diagnostics.EventLog.WriteEntry(“”, _strXdoc);

byte[] outBytes = System.Text.Encoding.ASCII.GetBytes(_strXdoc);
MemoryStream memStream = new MemoryStream();
memStream.Write(outBytes, 0, outBytes.Length);
memStream.Position = 0;

bodyPart.Data = memStream;
pContext.ResourceTracker.AddResource(memStream);

}
}
messageType = “http://NameSpace#RootNode”;
string systemPropertiesNamespace = @”http://schemas.microsoft.com/BizTalk/2003/system-properties”;
pInMsg.Context.Promote(“MessageType”, systemPropertiesNamespace, messageType);

return pInMsg;
}

#endregion

#region IPersistPropertyBag Members

public void GetClassID(out Guid classID)
{
classID = new Guid(“b94e8347-9516-4cbb-8c8b-dcf0789008c2”);
}

public void InitNew()
{

}

public void Load(IPropertyBag propertyBag, int errorLog)
{
object val = null;

try
{
propertyBag.Read(“NewNameSpace”, out val, 0);
}
catch (Exception ex)
{
throw new ApplicationException(“Error reading propertybag: ” + ex.Message);
}
if (val != null)
_NewNameSpace = (string)val;
else
_NewNameSpace = “http://AnonymusURL”;

}

public void Save(IPropertyBag propertyBag, bool clearDirty, bool saveAllProperties)
{
object val = (object)_NewNameSpace;
propertyBag.Write(“NewNameSpace”, ref val);

}

#endregion
}
}

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: