Our xml input file contains the customer elements. A customer is identified by an id and a simple name. Each customer itself has one or more payments methods. A payment method consists of a type, a number and optionally a valid to element.
We define a XmlSource to read the data from this file (XmlInputData.xml).
The parsing of an xml file is based on the System.Xml.XmlSerializer, and can be controlled by using various xml attributes. For parse the customer and payment methods into objects, we need to define our POCOs like this:
Now we can define the XmlSource using these objects and the input file name:
Note
Ignoring the DtdProcessing can be useful if your xml has DTD definitions that don't want to be processed (which can create new issues if your xml file is big). Setting the XmlReaderSettings allows you to manage how the xml is parsed.
The goal of this example is to flatten the payment method array so that it not only contains the payment data, but also the data of the containing customer element. So we can already define how our output POCO should look like.
As you can see, we want to get an object that holds all payment details, along with the customer data from the parent customer element. In this example, we will load our data structure into memory.
Next we use a RowMultiplication to flatten the nested array. The row multiplication takes one input row and can return multiple output rows as an array. (The array can also be empty). This transformation accepts two type: The type of the ingoing data (Customer) and the type of the outgoing data PaymentMethodAndCustomer. The property RowMultiplicationFunc is a delegate that has the currently processed customer row as an input object and return an array of newly created payment and customer objects.