Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serialize xml different elements to several properties in c#

I have next xml:

<table-display-fields>
  <field name="NAME/>
  <field name="DESCRIPTION" />
</table-display-fields>

I deserealize that with next code:

[XmlArray("table-display-fields")]
[XmlArrayItem("field")]
public TableDisplayField[] TableDisplayFields;

Then I add new xml element to table-display-fields node:

<table-display-fields>
  <record-number-field name="ID" />
  <field name="NAME/>
  <field name="DESCRIPTION" />
</table-display-fields>

Then add next code to deserealize record-number-field:

[XmlArray("table-display-fields")]
[XmlArrayItem("record-number-field")]
public TableDisplayField[] RecordTableDisplayFields;

[XmlArray("table-display-fields")]
[XmlArrayItem("field")]
public TableDisplayField[] TableDisplayFields;

This doesn't work. How do I deserealize new xml, and save the existing property path?

like image 607
tbicr Avatar asked Sep 26 '11 15:09

tbicr


1 Answers

You must remove XmlArrayItem() attribute.

[XmlArray("table-display-fields")] 
public object[] TableDisplayItems;

Each object in the TableDisplayItems will be either a field or a record-number-field.

Of course, if you only have one single record-number-field on top of your array, the solution can be much nicer. Is it the case ?

like image 109
Serge Wautier Avatar answered Sep 25 '22 15:09

Serge Wautier