0

I have an XML UI Definition which has a xsd schema definition which I have translated and created c sharp classes describing each of the elements so that I can represent my UI as an object and serialize it.

I used https://github.com/mganss/XmlSchemaClassGenerator to get me started which built my classes.

What I can't work out how to do is the controls section (example below)

The child elements of this section are an ordered list of any of the components. The elements can appear multiple times within the controls element.

<controls>
      <subheader id="subheaderOutletID" label="^subHeaderPduControls" />
      <button id="pduSettingsStatusOnButtonId" label="^OutletOn" />
      <button id="pduSettingsStatusOffButtonId" label="^OutletOff" />
      <button id="pduSettingsStatusResetButtonId" label="^OutletReset" />
      <subheader id="subheaderOutlet2ID" label="^subHeaderPduState" />
      <button id="pduSettingsStateId" label="{currentPduState}" icon="{currentPduImage}" />
      <button id="pduSettingsVoltageId" label="{currentPduStateVoltage}"/>
      <button id="pduSettingsCurrentId" label="{currentPduStateCurrent}"/>

      <subheader id="subheaderOutlet3ID" label="^subHeaderPduChangeName" />
      <textentry id="pduSettingsTextEntryId" label="$pdu.Name" value="{updateOutletName}" />
      <checkbox id="pduShowPublic" label="^PduShowCheck" value="{currentPduPublicDisplay}"  />
                
</controls>

The class generator builds within the controls object a collection for each of the control types for e.g.

        [System.Xml.Serialization.XmlElementAttribute("toggleslider", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public System.Collections.ObjectModel.Collection<Toggleslider> Toggleslider
        {
            get
            {
                return this._toggleslider;
            }
        }

But When serializing the xml this creates a list for each element type ... evidently

e.g.

<controls>
      <subheader id="subheaderOutletID1" label="^subHeaderPduControls" />
      <subheader id="subheaderOutletID2" label="^subHeaderPduControls" />
      <subheader id="subheaderOutletID3" label="^subHeaderPduControls" />
      <button id="pduSettingsStatusOnButtonId1" label="^OutletOn" />
      <button id="pduSettingsStatusOnButtonId2" label="^OutletOn" />
      <button id="pduSettingsStatusOnButtonId3" label="^OutletOn" />
</controls>

I have tried to create a Collection of elements which derive from a common type.

Which gets me some of the way but the xml tag is then the name of my common base not the child element.

e.g.

<controllist xsi:type="subheader" id="Test" />
<controllist xsi:type="button" id="Test" />
<controllist xsi:type="subheader" id="Test2" />
<controllist xsi:type="button" id="Test2" />

Whereas I want this list to produce

<subheader  id="Test" />
<button id="Test" />
<subheader  id="Test2" />
<button  id="Test2" />

Any suggestions for how to create this dynamic structure would be appreciated.

  • *I have tried to create a Collection of elements which derive from a common type... Which gets me some of the way but the xml tag is then the name of my common base not the child element.* - then can you [edit] your question to share what did not work, i.e. a [mcve]? – dbc Feb 15 '21 at 13:45
  • Probably you need to replace some `XmlInclude` declarations with `[XmlElement("subheader", Type = typeof(Subheader)), [XmlElement("button", Type = typeof(Button)), /* other types as required */]` on your `controls` collection as shown in [this answer](https://stackoverflow.com/a/1643424/3744182) to [Using XmlSerializer to serialize derived classes](https://stackoverflow.com/q/1643139/3744182). However `XmlArrayItem` might be required instead depending on the details of your data model. – dbc Feb 15 '21 at 13:45
  • In fact this could be a duplicate of [Using XmlSerializer to serialize derived classes](https://stackoverflow.com/q/1643139/3744182), agree? – dbc Feb 15 '21 at 13:56

0 Answers0