BizTalk CustomPipeline Component to add NameSpace

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.BizTalk.Component.Interop;
using Microsoft.BizTalk.Message.Interop;
using System.IO;
using System.Text.RegularExpressions;
using System.Xml;
using Microsoft.BizTalk.Streaming;

namespace AddUpdateNameSpace.PipelinesComponent
{
[ComponentCategory(CategoryTypes.CATID_PipelineComponent)]
[ComponentCategory(CategoryTypes.CATID_Any)]
[System.Runtime.InteropServices.Guid(“675CBE6B-E098-4DB8-97F0-D6A9A4183288”)]
public class AddNameSpaceComponent : IBaseComponent, IComponentUI, IComponent, IPersistPropertyBag
{
#region IBaseComponent Members
public string Description
{
get
{
return “Pipeline component used to add namespaces to XML Element.”;
}
}

public string Name
{
get
{
return “AddXMLNameSpace”;
}
}

public string Version
{
get
{
return “1.0.0.0”;
}
}
#endregion

#region IComponentUI Members
public IntPtr Icon
{
get
{ return new System.IntPtr(); }
}

public System.Collections.IEnumerator Validate(object projectSystem)
{
return null;
}
#endregion

#region IPersistPropertyBag Members

private string _NamespacePreifix = string.Empty;
private string _TargetNamespace = string.Empty;
public string NamespacePreifix
{
get { return _NamespacePreifix; }
set { _NamespacePreifix = value; }
}
public string TargetNamespace
{
get { return _TargetNamespace; }
set { _TargetNamespace = value; }
}
public void GetClassID(out Guid classID)
{
classID = new Guid(“675CBE6B-E098-4DB8-97F0-D6A9A4183288”);
}

public void InitNew()
{
}

public void Load(IPropertyBag propertyBag, int errorLog)
{
try
{
object val = null;
val = this.ReadPropertyBag(propertyBag, “NamespacePreifix”);
if (val != null)
this._NamespacePreifix = ((string)val);

object val1 = null;
val1 = this.ReadPropertyBag(propertyBag, “TargetNamespace”);
if (val1 != null)
this._TargetNamespace = ((string)val1);
}
catch (Exception ex)
{
string exMsg = null;
throw new ApplicationException(exMsg);
}
}

private object ReadPropertyBag(Microsoft.BizTalk.Component.Interop.IPropertyBag pb, string propertyName)
{
object val = null;
try
{
pb.Read(propertyName, out val, 0);
}
catch (ArgumentException)
{
return val;
}
catch (Exception ex)
{
string exMsg = null;
throw new ApplicationException(exMsg);
}
return val;
}

public void Save(IPropertyBag propertyBag, bool clearDirty, bool saveAllProperties)
{
try
{

object val = ((string)this._NamespacePreifix);
propertyBag.Write(“NamespacePreifix”, ref val);

object val1 = ((string)this._TargetNamespace);
propertyBag.Write(“TargetNamespace”, ref val);
}
catch (Exception ex)
{
string exMsg = null;
throw new ApplicationException(exMsg);
}
}
#endregion

#region IComponent Members
public IBaseMessage Execute(IPipelineContext pContext, IBaseMessage pInMsg)
{
IBaseMessageContext _msgContext = pInMsg.Context;
Stream originalStream = new MemoryStream();
MemoryStream memStream = new MemoryStream();
IBaseMessage OutMsg = null;
IBaseMessagePart msgPart;
msgPart = pInMsg.BodyPart;

#region AddOrUpdateNamespaces
try
{
if (msgPart != null)
{
originalStream = pInMsg.BodyPart.GetOriginalDataStream();
if (originalStream != null)
{
if (this.NamespacePreifix != null && this.TargetNamespace != null)
{
pInMsg.BodyPart.Data = new AddXMLNamespace(originalStream)
{
NamespaceURI = this.TargetNamespace,
NamespacePrefix = this.NamespacePreifix
};
}

}
}

}
catch (Exception ex)
{
System.Diagnostics.EventLog.WriteEntry(“PipelineError”, “PipelineError”);
}
#endregion

return pInMsg;
}
#endregion

#region Override XmlTranslatorStream
/// Implements XmlTranslatorStream Class
public class AddXMLNamespace : XmlTranslatorStream
{
public string NamespaceURI { get; set; }

public string NamespacePrefix { get; set; }

protected override void TranslateStartElement(string prefix, string localName, string nsURI)
{
base.TranslateStartElement(NamespacePrefix, localName, NamespaceURI);
}

protected override void TranslateAttribute()
{
if (this.m_reader.Prefix != “xmlns”)
base.TranslateAttribute();
}

public AddXMLNamespace(Stream input)
: base(new XmlTextReader(input), Encoding.Default)
{
}
protected override void TranslateXmlDeclaration(string target, string value)
{
this.m_writer.WriteProcessingInstruction(this.m_reader.Name, this.m_reader.Value);
}
}

#endregion
}
}

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