BizTalk, Pipeline Components

Removing Xml Namespace in a pipeline component

Richard Hallgren recently blogged about how to remove Xml namespaces from Xml documents, as has Kirk Allen Evans earlier here and here. While the latter writes from a general .NET and ASP.NET perspective Richard’s post is from a BizTalk perspective and in his post he asks for alternative ways, other then the ways he is presenting, of doing it. 


The root issue is how do I turn

<ns0:Blah xmlns:ns0=”http://RemoveXmlNamespace.BTS.BlahMessage”>

into

<Blah>


First of all, let me just say that I understand namespace and I don’t think they should be removed if at all avoidable. This is not my first choice of a solution, as it probably isn’t for anyone working with BizTalk. The fact remains though that in some cases it still turns out to be necessary. My option of doing this would then be by using a pipeline component and the classes available to us from Microsoft.BizTalk.Streaming.dll. My main reason for this is performance. I would really hate to have to keep either the source document or the resulting document in memory using XmlDocument and MemoryStream like the XslTransform pipeline component sample from the BizTalk Server 2006 SDK does. I’m pretty certain that particular sample can be enhanced using XPathDocument, XslCompiledTransform and VirtualStream (to keep down the impact on memory) but it still isn’t as good if your only purpose is removing the namespace. That sample can however do many other things that my option can’t, since it does an XslTransform and this doesn’t.


So using the streaming classes in general and XmlTranslatorStream in particular we can override some of it’s methods to create a streaming xsl transformation doing what we want. The resulting code is suprisingly simple. Here is the Execute method:

public Microsoft.BizTalk.Message.Interop.IBaseMessage Execute(
IPipelineContext pContext,
Microsoft.BizTalk.Message.Interop.IBaseMessage pInMsg)
{
pInMsg.BodyPart.Data = new XmlNamespaceRemoverStream(
pInMsg.BodyPart.GetOriginalDataStream());
return pInMsg;
}

And here is the class that does the actual work (although as you can see it doesn’t really do much):

public class XmlNamespaceRemoverStream : XmlTranslatorStream
{
protected override void TranslateStartElement(
string prefix, string localName, string nsURI)
{
base.TranslateStartElement(null, localName, null);
}

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

public XmlNamespaceRemoverStream(Stream input)
: base(new XmlTextReader(input), Encoding.Default)
{ }
}


This sample is available for download: removexmlnamespacepipeline.zip.

1 thought on “Removing Xml Namespace in a pipeline component”

  1. Hi, I also added an override to TranslateAttributeValue:
    protected override void TranslateAttributeValue(string prefix, string localName, string nsURI, string val)
    {
    string newValue = val.Remove(0, val.IndexOf(‘:’) + 1);
    base.TranslateAttributeValue(null, localName, nsURI, newValue);
    }

    Like

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 )

Facebook photo

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

Connecting to %s