Biz(Talk)2

Talk, talk and more talk about BizTalk

Changing / Removing document namespaces

I recently ran into a requirement to both transmit and receive documents using “Plain Old Xml”. The system we were integrating was not able to understand the concept of namespaces in an Xml document, but BizTalk requires them, as messages are determined by the combination of a document’s Namespace and Root Node.

The solution was two-fold. For incoming messages, we “inject” a pre-determined namespace into the document as soon as it hits BizTalk. For outbound messages, we strip out all namespaces and alias’ just as we are sending it. The best was to do this is using a Pipeline Component.

There are a number of components and methods out there for adding a namespace to an inbound messages, see here and here.  I ended up re-using a sample I found on CodePlex.

To remove namespaces from the outbound messages, I massaged the content of the message using a Regular Expression, and some simple text replacement:

private static string RemoveNamespace(XmlDocument xdoc)
{
  // Regex to search for either xmlns:nsN="..." or xmlns="..."
  const string pattern = @"xmlns:?(\w+)?=""[\w:/.]*""";
  string xml = xdoc.OuterXml;

  Regex replace = new Regex(pattern);
  // Replace all occurances of the namespace declaration
  string temp = replace.Replace(xml, String.Empty);
  foreach (Match match in replace.Matches(xml))
  {
    // Loop through each Match in the Regex, this gives us the namespace aliases found
    if (match.Success && match.Groups.Count > 1)
    {
      if (!String.IsNullOrEmpty( match.Groups[1].Value))
      {
        Regex alias = new Regex(@"\b" + match.Groups[1].Value + ":");
        // Replace all occurances of the
        temp = alias.Replace(temp, String.Empty);
      }
    }
  }
  return temp;
}

The regular expression identifies namespace declarations, whether they declare an alias or not.  We then iterate all of the matched aliases, removing them from the target xml document.

Edit: I have added a link to cut-down sample project, detailing exactly how the pipeline component should be implemented.  Click here to download.

Edit2: Modified the regex pattern slightly so it also accounts for elements declared in the “empty” namespace.

Advertisements

June 26, 2012 - Posted by | BizTalk, Pipeline | , ,

3 Comments »

  1. “To remove namespaces from the outbound messages, I massaged the content of the message using a Regular Expression, and some simple text replacement:”

    Woah woah woah woah! How? Where did you put that code? That would make this infinitely more helpful. Did you make this part of a custom pipeline component? Do you just have some service running somewhere that executes this code? Where is this used??

    Comment by Matt Scheaffer | July 26, 2012 | Reply

  2. “To remove namespaces from the outbound messages, I massaged the content of the message using a Regular Expression, and some simple text replacement:”

    Can you please tell me WHERE you used this code? I don’t get where you put this code to make it run during the biztalk process.

    Comment by Matt | July 26, 2012 | Reply

  3. DId you make that code in a custom pipeline component or something?

    Comment by Matt | July 26, 2012 | Reply


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: