first time poster, long time reader. I've got an XML import to write (another first), and i'm getting lost in the conflicting methods and approaches that can be adopted.
The XML I'm trying to Import looks roughly like this:
...
<TableRow>
<Field Name="DeliveryNote" Type="etc..">Text</Field>
<Field Name="OrderNumber" Type="etc..">Text</Field>
and so on...
</TableRow>
...
I have an object with Properties for the majority of the fields (not all required), and I'm currently populating the properties in a constructor that is thrown the relevant XElement to build from, like so:
public Order(XElement p_tableRow)
{
foreach (XElement field in p_TableRow.Elements("Field"))
{
switch (field.Attribute("Name").Value)
{
case "DeliveryNote":
ReasonCode = field.Value;
break;
case "OrderNumber":
OrderID = field.Value;
break;
case "ProductCode":
CustProductCode = field.Value;
break;
case "Quantity":
QuantityCharged = field.Value;
break;
...
My problem is that whilst this gets the job done, it seems awfully clunky (and ugly). Surely there is a better way?
I've tried getting my head around how to do this with projection but haven't found an example where you're deciding the destination of the data based on an attribute in the node.
I'm using .NET 3.5
Any pointers would be massively appreciated.