Introduction

The sample BizTalk Custom Pipeline Component is used to Add or Update or Remove qualified target namespace, Qualified Prefix for any incoming or outgoing XML file.

Problem

  • Suppose there can be a scenario like you are getting a plan XML message without target namespace, you need to add qualified target namespace, process the message. OR
  • You have a schema that is common to one or more partners to disassemble the message against common schema structure, just the target namespace is the difference. OR
  • There can be a scenario where you need to remove target namespace, process plain XML message or without a target namespace. OR
  • Similar Issue faced on MSDN BizTalk Forum Click Here

Pipeline Component Solution

  • The scenario is to update the message using pipeline component.
  • Have used XmlTranslatorStream class to achieve this scenario.
  • Here is the sample solution to achieve the same to Add or Remove Qualified Target Namespace.

Add or Update

  • The below sample component configuration sample shows to Add or Update the target namespace information.
  • Calling the below method in component updates Target Namespace, Prefix for every element.
  • Code Blocks Used
#region AddOrUpdateNamespaces
if (this.NamespacePreifix != null && this.TargetNamespace != null)
           {
inmsg.BodyPart.Data = new AddXMLNamespace(inmsg.BodyPart.GetOriginalDataStream())
              {
NamespaceURI = this.TargetNamespace,
NamespacePrefix = this.NamespacePreifix
};
}
#endregion

Code Block Used in Execute Method 

#region Override XmlTranslatorStream
 
        /// <summary>
        /// Implements XmlTranslatorStream Class
        /// </summary>
        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
 

Code Block Used as a Method to call in Execute Method

  • Pipeline Component Configuration

Fig – Pipeline Component Configuration

  • Input, Output Message


Fig - Sample Input without, with a namespace to with target namespace, Prefix for every element.

Remove

  • The below sample component configuration sample shows to Remove the target namespace information.
  • Calling the below method in component removes or updates to null for Target Namespace, Prefix for every element.
  • Code Block
#region RemoveNamespaces
 
               if (this.NamespacePreifix == null && this.TargetNamespace == null)
               {
                   inmsg.BodyPart.Data = new RemoveXMLNamespace(inmsg.BodyPart.GetOriginalDataStream());
               }
 
#endregion


Code Block Used in Execute Method 

#endregion
 
       #region override XmlTranslatorStream
 
       /// <summary>
       /// Implements XmlTranslatorStream Class
       /// To Remove the Namespaces completely
       /// </summary>
       public class RemoveXMLNamespace : 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 RemoveXMLNamespace(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

Code Block Used as a Method to call in Execute Method
  • Pipeline Component Configuration


Fig – Pipeline Component Configuration
  • Input, Output Message


Fig - Sample Input with namespace to Plain XML without namespaces

Source Code

  • To work with this example, You can download the Pipeline Component, Source Code here MSDN TechNet Gallery.

Build Deploy Test

  • Import, Install the MSI.
  • Import the Bindings, Update Folder path.


Fig – MSI Package Includes Sample Schema, Receive Pipeline, Pipeline Component.

  • GAC the Component DLL.
  • Place a copy of the file Component dll here "C:\Program Files (x86)\Microsoft BizTalk Server 2013 R2\Pipeline Components"
  • Start the BizTalk application.
  • Restart BizTalk Host Instances.
  • Drop the sample file, Observe the output like shown in below images.

Conclusion

  • In this article, you have learned how to use XmlTranslatorStream Class to Add or Update the Target Namespace, Prefix for the incoming XML file.
  • Also, you can use this component to Remove the Target Namespace, Prefix i.e. to make to a simple plain XML file without namespace.

See Also


↑ Return to Top