none
Need help to add prefix for all the elements in pipeline component. RRS feed

  • Question

  • Hi,

    I Need to set prefix for all records and elements ( i am using visual studio 2005)

    Below is my input XML file with out prefix,namespace, for all records and elements.

    <?xml version="1.0" encoding="utf-8" ?> 
     <Root>
      <Data>
      <Name>Name</Name> 
      <Age>26</Age>  
      </Data>
    </Root>

    Below is expected output file.

    <?xml version="1.0" encoding="utf-8" ?> 
      <ns0:Root xmlns:ns0="http://abc.com">
         <ns0:Data>
           <ns0:Name>Name</ns0:Name> 
           <ns0:Age>26</ns0:Age>  
        </ns0:Data> 
    </ns0:Root>

    Much appreciated

    Regards,

    Surendra


    Friday, March 21, 2014 8:01 AM

Answers

  • Hi Surendra,

    I have understood your requirement here.

    To add Target Namespace, Prefix to each records and elements, you need to use the XmlTranslatorStream class to achieve this scenario (Microsoft.BizTalk.Streaming.dll).

    Please use the below code in your pipeline component to achieve the requested scenario.

    1) Adding XML Target Namespace, Prefix for each element using a custom pipeline component.

    #region IComponent Members
    
    public IBaseMessage Execute(IPipelineContext pContext, IBaseMessage pInMsg)
            {
                try
                {
                    pInMsg.BodyPart.Data = new AddXMLNamespace(pInMsg.BodyPart.GetOriginalDataStream());   
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                return pInMsg;
            }
    
    #endregion

    Here is the main code which will override the methods to add or to remove the namespaces.

    #region override XmlTranslatorStream
    
    public class AddXMLNamespace : XmlTranslatorStream
            {
                protected override void TranslateStartElement(
                    string prefix, string localName, string nsURI)
                {
                    base.TranslateStartElement("ns0", localName, "http://abc.com");
                }
    
                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

    2) Removing XML Target Namespace, Prefix for each element using a custom pipeline component.

    #region override XmlTranslatorStream
    
    // To Remove the Namespaces completely
    
    public class AddXMLNamespace : 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 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

    Thanks, SMS Vikas K

    _______________________________________________________________________________

    If this post is helpful, please vote as helpful by clicking the upward arrow mark next to my reply.


    • Proposed as answer by anilraos Wednesday, March 26, 2014 1:10 PM
    • Marked as answer by Surendra Kurmam Thursday, March 27, 2014 5:57 AM
    Wednesday, March 26, 2014 10:42 AM
    Answerer

All replies

  • To get the output desired, the destination schema should have the default element/attribute form set to "Qualified".

    If you create an input schema (no namespace) and create an output schema (with target namespace) and set the element form to "qualified", a simple map from the input to output will generate what you're looking for.

    Regards.

    Friday, March 21, 2014 8:44 AM
  • Hi,

    Thanks for you reply......

    I need to do this in pipeline component before disassemble stage. i need c# code to this.

    Regards,


    Friday, March 21, 2014 8:50 AM
  • There are existing components to help you do that.

    Refer http://msdn.microsoft.com/en-us/library/ee250047(v=bts.10).aspx

    If you wish to write your own then refer to Richard Seroter's article on the same @http://seroter.wordpress.com/2007/02/11/add-namespace-to-inbound-biztalk-messages/

    So you are receiving a message without name space and in the pipeline you want this to be converted into a full qualified /w namespace xml?

    Regards.

    Friday, March 21, 2014 9:12 AM
  • I am looking for a c# code which will prefix “ns0” to all the xml elements (similar to qualified property in schema).

     

    I have a schema for the below message type, I need to modify the xml content to below format so that I can disassemble with the schema

     

    <?xml version="1.0" encoding="utf-8" ?>    <ns0:Root xmlns:ns0=”http://abc.com">      <ns0:Data>        <ns0:Name>Name</ns0:Name>         <ns0:Age>26</ns0:Age>       </ns0:Data>  </ns0:Root>

     

    I have tried this much in console application, need help in looping through each element to add prefix “ns0”

     

    XmlDocument doc = new XmlDocument();

    doc.Load(@"E:\XMLFile.xml");

    doc.DocumentElement.SetAttribute("xmlns:ns0", "http://abc.com");

         

    doc.Save("NewDoc.xml");

    Console.ReadKey();


    Friday, March 21, 2014 9:34 AM
  • Hi Surendra,

    What I can see from your post that you need to fix Namespace in your incoming request xml . for this purpose there is already a pipeline component to add namespace to existing xml over MSDN . You can use it  to fix your incoming message

    Just you need to use it asyour receive pipeline component

    http://code.msdn.microsoft.com/Biztalk-change-RootNode-c19c84ee/

    http://www.codeproject.com/Articles/146182/Change-Namespace-of-a-Message-in-a-Custom-Pipeline

     public IBaseMessage Execute(IPipelineContext pContext, IBaseMessage pInMsg)
            {
                //To get Incoming message
                System.IO.Stream originalStream = pInMsg.BodyPart.GetOriginalDataStream();
                //Working with XDocument
                XDocument xDoc;
                using (XmlReader reader = XmlReader.Create(originalStream))
                {
                    reader.MoveToContent();
                    xDoc = XDocument.Load(reader);
                }
                xDoc.Root.RemoveAttributes();
                xDoc.Root.Add(new XAttribute(XNamespace.Xmlns + "ns0", Namespace));
                xDoc.Root.Name = xDoc.Root.GetNamespaceOfPrefix("ns0") + Rootnode;
                // Returning stream
                byte[] output = System.Text.Encoding.ASCII.GetBytes(xDoc.ToString());
                MemoryStream memoryStream = new MemoryStream();
                memoryStream.Write(output, 0, output.Length);
                memoryStream.Position = 0;
                pInMsg.BodyPart.Data = memoryStream;
                return pInMsg;
            }
            #endregion

    Thanks

    Abhishek


    Friday, March 21, 2014 9:42 AM
  • Hi Surendra,

    I have understood your requirement here.

    To add Target Namespace, Prefix to each records and elements, you need to use the XmlTranslatorStream class to achieve this scenario (Microsoft.BizTalk.Streaming.dll).

    Please use the below code in your pipeline component to achieve the requested scenario.

    1) Adding XML Target Namespace, Prefix for each element using a custom pipeline component.

    #region IComponent Members
    
    public IBaseMessage Execute(IPipelineContext pContext, IBaseMessage pInMsg)
            {
                try
                {
                    pInMsg.BodyPart.Data = new AddXMLNamespace(pInMsg.BodyPart.GetOriginalDataStream());   
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                return pInMsg;
            }
    
    #endregion

    Here is the main code which will override the methods to add or to remove the namespaces.

    #region override XmlTranslatorStream
    
    public class AddXMLNamespace : XmlTranslatorStream
            {
                protected override void TranslateStartElement(
                    string prefix, string localName, string nsURI)
                {
                    base.TranslateStartElement("ns0", localName, "http://abc.com");
                }
    
                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

    2) Removing XML Target Namespace, Prefix for each element using a custom pipeline component.

    #region override XmlTranslatorStream
    
    // To Remove the Namespaces completely
    
    public class AddXMLNamespace : 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 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

    Thanks, SMS Vikas K

    _______________________________________________________________________________

    If this post is helpful, please vote as helpful by clicking the upward arrow mark next to my reply.


    • Proposed as answer by anilraos Wednesday, March 26, 2014 1:10 PM
    • Marked as answer by Surendra Kurmam Thursday, March 27, 2014 5:57 AM
    Wednesday, March 26, 2014 10:42 AM
    Answerer
  • Hi SMSVikas K,<o:p></o:p>

    I Just wondered by seeing your post. Exactly I am expecting this solution. Thanks for sharing.<o:p></o:p>


    Wednesday, March 26, 2014 10:56 AM