Skip to the content

Generating a dynamic XML result from an eForm

There is a easy way how to return any XML from an eform generated by the code at a time, when the client calls JS API functions like GetTransformedData and GetTransformedDataID. The trick lies with overriding the event named BeforeDataSend on ThisForm object.

At first it is necessary to register an event handler:

The code below demonstrates by simple example how the handled event BeforeDataSend could look like:

public static void BeforeDataSend(object sender, Neurodot.Forms.Render.Interfaces.FormDataTransformEventArgs e)
    {
        e.Data.Root.Add(new XElement("Content",  
        new XElement("Child1", "data1"),  
        new XElement("Child2", "data2"),  
        new XElement("Child3", "data3"),  
        new XElement("Child2", "data4"),  
        new XElement("Info5", "info5"),  
        new XElement("Info6", "info6"),  
        new XElement("Info7", "info7"),  
        new XElement("Info8", "info8")  
    ) );
    }

The approach above requires to configure any schema in the form, at least the root element of the schema. Schema base, at design time, can look like similar to this:

The name and namespace of the root schema element is not important, unless the code is written to use it. The example above is persisting the root name and its namespace and it is just adding child nodes to it.

 

The result of the GetTransformedData method is this sample would be the following:

<Message xmlns="urn:demo:dynamic:xml">
<Content xmlns="">
<Child1>data1</Child1>
<Child2>data2</Child2>
<Child3>data3</Child3>
<Child2>data4</Child2>
<Info5>info5</Info5>
<Info6>info6</Info6>
<Info7>info7</Info7>
<Info8>info8</Info8>
</Content>
</Message>