Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7959459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:34:37+00:00 2026-06-04T04:34:37+00:00

What’s the recommended way of adding namespaces to an incoming XML message which needs

  • 0

What’s the recommended way of adding namespaces to an incoming XML message which needs to be debatched? In order to debatch the incoming XML message I’m using an envelope as well as a “normal” message xsd schema which both have a target namespace. The incoming XML message doesn’t have any namespaces attached in the first place.

Thank you

using System;
using System.Linq;
using System.Xml.Linq;

public class AddNamespaceRcvPipelineComponent
{
    static public void Main ()
    {
        XElement xDoc = XElement.Load(@"test.xml");

        XNamespace ns1 = "Namespace1";
        XNamespace ns2 = "Namespace2";

        foreach (XElement el in xDoc.DescendantsAndSelf("ORDERS"))
        {
            // el.Add(new XAttribute(XNamespace.Xmlns + "ns1", "Namespace1"));
            el.Name = ns1 + el.Name.LocalName;
        }

        foreach (XElement el in xDoc.DescendantsAndSelf("ORDER"))
        {
            // el.Add(new XAttribute(XNamespace.Xmlns + "ns2", "Namespace2"));
            el.Name = ns2 + el.Name.LocalName;
        }

        // TODO: Strip empty namespaces ...

        Console.WriteLine(xDoc);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T04:34:37+00:00Added an answer on June 4, 2026 at 4:34 am

    The canonical way to solve your problem is indeed to use a custom Pipeline Component in the Decode Stage of the incoming pipeline.

    Here are two ways to get this working efficiently.

    Using the BizTalk ESB Tookit Pipeline Component

    The BizTalk ESB Toolkit 2.1 has a very powerful set of pipeline components to manipulate XML namespaces in incoming messages. This pipeline is available in the Microsoft.Practices.ESB.Namespace.PipelineComponents.dll assembly, made available to the Visual Studio toolbox when installed.

    Previous versions of these components are documented on MSDN. Although this documentation is quite outdated, I think nothing much has changed in the new version. I nonetheless suggest you refer to various forum questions for proper usage and reference.

    Building a Simple AddXmlNamespace Pipeline Component

    If you don’t want to or cannot install BizTalk ESB Toolkit in your environment, I suggest building a simplified version of the component yourself. It’s not hard at all, thanks to a builtin class in the BizTalk runtime that you can leverage.

    I’m showing the code required below, because the code you showed in your question does not adhere to proper streaming-enabled pipeline components. Please, note that the code shown hereafter only deals with adding an XML namespace on the original root tag, if one is not already present.

    First, you’ll need to build a simple System.IO.Stream-derived class that handles adding an XML namespace and prefix on the root tag of the original document.

    In order to support streaming, the code leverages the Microsoft.BizTalk.Streaming.XmlTranslatorStream class. This class exhibits a SAX-like interface, whereby overrides on your implementation are invoked at various points of the XML parsing stage. All of this is performed while maintaining full support for streaming arbitrary large documents.

    This is the code :

    using Microsoft.BizTalk.Streaming;
    
    public class AddXmlNamespaceStream : XmlTranslatorStream
    {
        private String namespace_;
        private int level_ = 0; // hierarchy level
    
        public AddXmlNamespaceStream(Stream stream, String @namespace)
            : base(XmlReader.Create(stream))
        {
            namespace_ = @namespace;
        }
    
        #region XmlTranslatorStream Overrides
    
        protected override void TranslateStartElement(string prefix, string localName, string nsURI)
        {
            if (level_++ != 0)
            {
                base.TranslateStartElement(prefix, localName, nsURI);
                return;
            }
    
            if (String.IsNullOrEmpty(nsURI))
            {
                nsURI = namespace_;
                if (String.IsNullOrEmpty(prefix))
                    prefix = "__bts_ns0__";
            }
    
            base.TranslateStartElement(prefix, localName, nsURI);
        }
    
        protected override void TranslateEndElement(bool full)
        {
            if (level_-- != 0)
            {
                base.TranslateEndElement(full);
                return;
            }
    
            base.TranslateEndElement(full);
        }
    
        #endregion
    }
    

    You’ll notice that this class overrides both the TranslateStartElement and TranslateEndElement methods but only deals with elements at the first level of hierarchy – which is the root tag. All other elements are processed according the default do-nothing-special behavior provided by the base class.

    As for the pipeline component itself, I will only show its Execute method here, since you probably have already setup all the required boilerplate infrastructure code. If that’s not the case, please refer to the blog posts at the following location, starting from the bottom.

    Here it is:

    public class AddXmlNamespace : ..., IComponent
    {
        #region Design-Time Properties
    
        public String Namespace { get; set; }
    
        #endregion
    
        #region IComponent Implementation
    
        public IBaseMessage Execute(IPipelineContext pContext, IBaseMessage pInMsg)
        {
            var stream = new AddXmlNamespaceStream( 
                pInMsg.BodyPart.GetOriginalDataStream()
                , Namespace);
    
            pInMsg.BodyPart.Data = stream;
            pContext.ResourceTracker.AddResource(stream);
    
            return pInMsg;
        }
    
        #endregion
    
        ...
    }
    

    As you see, the only action taken by the Execute method is to wrap the original stream in an instance of the new AddXmlNamespace stream class, and wire it up so that it replaces the incoming message stream.

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We are using XSLT to translate a RIXML file to XML. Our RIXML contains
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
In my XML file chapters tag has more chapter tag.i need to display chapters
I would like to run a str_replace or preg_replace which looks for certain words

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.