Removing Xml Namespace in a pipeline component

Listen with webReader
Published 07 January 08 11:11 PM | Johan Hedberg

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.

Comments

# Kirk Allen Evans said on January 8, 2008 12:22 AM:

That's sweet!  Nice solution, and easy to comprehend as well (much more so than the XmlNoNamespaceWriter, which took me 2 times to get right).

# Richard Hallgren said on January 8, 2008 03:41 PM:

Nice! Keep up the good work!

# Big Kev is still excited said on April 28, 2008 10:13 PM:

In the Part 4 I covered the XmlTranslatorStream in detail, explaining how it provides powerful coupling

#   Removing namespace from outgoing messages by .RICHARD said on June 17, 2008 08:29 AM:

Pingback from  &nbsp; Removing namespace from outgoing messages&nbsp;by&nbsp;.RICHARD

# Be Logical - writings by Johan Hedberg said on January 6, 2009 09:00 AM:

So, a year passed. Year-end statistics are always fun. However I really need to make sure we have additional

# KattyBlackyard said on June 15, 2009 09:07 AM:

Hi, gr8 post thanks for posting. Information is useful!

# Nema said on June 10, 2010 02:03 PM:

Hi, thanks for a great post. Do you know if there is any way to keep the xml declaration wich seems to be removed?

Leave a Comment

(required) 
(required) 
(optional)
(required) 

This Blog

News

    Messenger

    Twitter Updates

      Follow me on twitter

      Visitors

      Feedburner Subscribers

      Locations of visitors to this page

      Disclaimer

      All material is provided AS IS voiding any thinkable or unthinkable effect it might have for any use whatsoever. There... is that clear enough ;)

      Pages

    Syndication