Note that there are some explanatory texts on larger screens.

plurals
  1. POMapping Input fields to new output fields for serialization in c#
    primarykey
    data
    text
    <p>I mention C# in my title but this is a generic programming question. I am just looking to find the best approach to do the following:</p> <p>I have tableA which contains the following fields</p> <pre><code>ID : 1 ProjectName : "Test" City : "NY" Size : 100 Location : "USA" </code></pre> <p>Now I need to serialize that table for a 3rd party application however I the application uses a generic data contract (see below) and in addition the field names would have to be slightly modified. So the above table would be serialized as follows:</p> <pre><code>&lt;Object&gt; &lt;type&gt;Project&lt;/type&gt; &lt;Fields&gt; &lt;Key&gt;NameofProject&lt;/Key&gt;&lt;Value&gt;Test&lt;/value&gt; &lt;Key&gt;ProjectCity&lt;/Key&gt;&lt;Value&gt;NY&lt;/value&gt; &lt;Key&gt;ProjectLocation&lt;/Key&gt;&lt;Value&gt;USA&lt;/value&gt; &lt;/Fields&gt; &lt;/Object&gt; </code></pre> <p>As you can see I needed to map the fields from TableA into new field names/keys:</p> <pre><code>ProjectName -&gt; NameofProject City -&gt; ProjectCity Location -&gt;ProjectLocation </code></pre> <p>This is how I would achieve the above: - Create a mapping table which stores the mapping relationships as described above</p> <pre><code>InputField -&gt; OutputKey ProjectName -&gt; NameofProject City -&gt; ProjectCity Location -&gt;ProjectLocation I would then load the data from this table into a Set I would then loop through each row of the input - for each column in the row I would get the OutputKey based on the InputField as stored in my mapping Set, and build the xml </code></pre> <p>I assume storing a small amount of info in a Set and then doing the lookups for each field shouldnt be too bad. But I am not an experienced C# user so am wondering if there is a better way to do this in c#. Any help or direction would be greatly appreciated.</p> <p>EDIT: A comment suggested using XSLT - its a good idea but due to other complexities of this task its better for me to build the xml in code based on a data contract. Is there any way to achive the mapping via a sql join of the input data table and the mapping table?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload