Creating elements using a .NETCompute node

The NBElement class represents a single parsed element in the message tree. The class provides four different creation methods that you can use to create a new element in the message tree.

About this task

  • CreateFirstChild
  • CreateLastChild
  • CreateBefore
  • CreateAfter
The method names indicate where the element is to be created in the hierarchy in relation to the NBElement on which they are called. The following C# Evaluate method contains example code for a ..NETCompute node:
        public override void Evaluate(NBMessageAssembly inputAssembly)
        {
            NBOutputTerminal outTerminal = OutputTerminal("Out");
            NBMessage inputMessage = inputAssembly.Message;
            
            // Create a new empty message, ensuring it is disposed after use
            using (NBMessage outputMessage = new NBMessage())
            {
                NBMessageAssembly outAssembly = new NBMessageAssembly(inputAssembly, outputMessage);
                NBElement inputRoot = inputMessage.RootElement;
                NBElement outputRoot = outputMessage.RootElement;

                // Optionally copy message headers, remove if not needed
                CopyMessageHeaders(inputRoot, outputRoot);

                #region UserCode
                // Add user code in this region to create a new output message            
                NBElement msg = outputRoot.CreateLastChildUsingNewParser(NBParsers.XMLNSC.ParserName).CreateFirstChild(null,"Message");
                NBElement el4 = msg.CreateLastChild("Element4");
                NBElement el1 = msg.CreateFirstChild("Element1");
                el1.SetValue("Data Value for Element1");
                el4.SetValue("Data Value for Element4");
                el1.CreateAfter("Element2").SetValue("Data Value for Element2");
                el4.CreateBefore("Element3").SetValue("Data Value for Element3");                
                #endregion UserCode

                // Change the following if not propagating message to the 'Out' terminal
                outTerminal.Propagate(outAssembly);
            }
        }
When the node is wired to a suitable output node (such as an MQOutput node), the code produces an XML message that looks like this:
<Message>
     <Element1>Data Value for Elememt1</Element1>
     <Element2>Data Value for Elememt2</Element2>
     <Element3>Data Value for Elememt3</Element3>
     <Element4>Data Value for Elememt4</Element4>
</Message>

For information about the .NET classes and methods that are supported in IBM® App Connect Enterprise, see the .NET API documentation.