Parse XML to C#

Hi,

I have the following XML, that is quite complex and the elements and attributes can change based on what is and isn't set server side (See first XML config file). What I need to do is build a collection of systems. So, I need to find the 'Struct name="xxx" nodeType="System" ' element, which defines a 'System', and build up a collection of these. The 'Objects' of each system can be different, as it returns on the objects that are given a value server side (The first XML defined all available objects), otherwise the object isn't returned to the client and the element is not returned so I'm struggling to find a way to do this.

The complete list of 'Objects' that is ever returned is fixed and is shown in the first XML where 'Object Id' is the object that is returned into the 'Objects' collection with the datatype as the element name.

I'm assuming I'll be best to create a class with all possible elements from the first XML file and somehow populate this from the second xml that is returned from the server, but not sure where to get started with this


Open in new window

 <ObjectGroup displayName="Temperature">
    <Object id="TEMP" displayName="Temperature" objtype="AV,AI" hints="Temperature,Temp"/>
    <Object id="TEMP_SP" displayName="Setpoint" objtype="AV,AO" hints="Temperature,Temp,Setpoint,SP"/>
  </ObjectGroup>
  <ObjectGroup displayName="Overrides">
    <Object id="LOCALOVERRIDE" displayName="Local Override" objtype="BV" hints="Override"/>
    <Object id="LOCALOVERRIDETIME" displayName="Local Override Duration" objtype="AV" hints="Override"/>
    <Object id="LOCALOVERRIDEREMAIN" displayName="Local Override Remaining" objtype="AV" hints="Override"/>
  </ObjectGroup>
  <ObjectGroup displayName="ACU">
    <Object id="COMMAND" displayName="Enable Command" objtype="BO,BV" hints="ON_w,Command,Enable"/>
    <Object id="STATUS" displayName="Unit Status" objtype="BI,BV" hints="ON_r,Status"/>
    <Object id="FANCOMMAND" displayName="Fan Speed Command" objtype="MV" hints="Fan"/>
    <Object id="FANSTATUS" displayName="Fan Speed Status" objtype="MV,MI" hints="Fan"/>
    <Object id="MODECOMMAND" displayName="Mode Command" objtype="MV" hints="Mode"/>
    <Object id="MODESTATUS" displayName="Mode Status" objtype="MV,MI" hints="Mode"/>
    <Object id="ADCOMMAND" displayName="Air Direction Command" objtype="MV" hints="Air"/>
    <Object id="ADSTATUS" displayName="Air Direction Status" objtype="MV,MI" hints="Air"/>
  </ObjectGroup>
  <ObjectGroup displayName="Lighting">
    <Object id="LIGHTS1" displayName="Lights 1" objtype="AV,AO,BV,BO" hints="Light,Lighting"/>
    <Object id="LIGHTS2" displayName="Lights 2" objtype="AV,AO,BV,BO" hints="Light,Lighting"/>
    <Object id="LIGHTS3" displayName="Lights 3" objtype="AV,AO,BV,BO" hints="Light,Lighting"/>
    <Object id="LIGHTS4" displayName="Lights 4" objtype="AV,AO,BV,BO" hints="Light,Lighting"/>
    <Object id="SCENE" displayName="Lighting Scene" objtype="MV" hints="Light,Lighting"/>
  </ObjectGroup>
  <ObjectGroup displayName="Blinds">
    <Object id="BLINDS" displayName="Blinds" objtype="AV,AO,BV,BO" hints="Blinds"/>
  </ObjectGroup>
  <ObjectGroup displayName="Windows">
    <Object id="WINDOWCOMMAND" displayName="Window Mode (MV Only)" objtype="MV" hints="Window"/>
  </ObjectGroup>
{<Collection name="Virtual Stat" xmlns="http://bacnet.org/csml/1.2">
  <Collection name="Garden">
    <Struct name="Garden" nodeType="System">
      <String name="location" value="" />
      <String name="address" value="" />
      <String name="description" value="" />
      <String name="template" value="vstatBEC.xml" />
      <Struct name="Objects">
        <Real name="LIGHTS1" value="0" displayName="Zone 1 Lighting Current Level" description="" physical="/.bacnet/22 Quarry Road/900/analog-value,11" />
        <Real name="LIGHTS2" value="0" displayName="Zone 2 Lighting Current Level" description="" physical="/.bacnet/22 Quarry Road/900/analog-value,21" />
        <Real name="TEMP" value="15.5258" displayName="OAT_4_20mA" description="" physical="/.bacnet/22 Quarry Road/400000/analog-input,1102" />
      </Struct>
    </Struct>
    <Struct name="Default System Dashboard" nodeType="Other">
      <Enumerated name="Type" value="System" />
      <Enumerated name="Layout" value="2" />
      <Collection name="Widgets">
        <Struct name="1ae623e1-837f-11e3-b067-94de806b511c">
          <String name="Type" value="Point List" />
        </Struct>
        <Struct name="1ae623ed-837f-11e3-b067-94de806b511c">
          <String name="Type" value="Alarm List" />
        </Struct>
        <Struct name="1ae623fb-837f-11e3-b067-94de806b511c">
          <String name="Type" value="Alarm Summary" />
        </Struct>
      </Collection>
    </Struct>
  </Collection>
  <Collection name="Master Bedroom">
    <Struct name="Master Bedroom" nodeType="System">
      <String name="location" value="" />
      <String name="address" value="" />
      <String name="description" value="" />
      <String name="template" value="vstatBEC.xml" />
      <Struct name="Objects">
        <Unsigned name="ADCOMMAND" value="5" displayName="Master Bedroom (AD_w)" description="" physical="/.bacnet/22 Quarry Road/200/multi-state-output,100113" />
        <Unsigned name="ADSTATUS" value="5" displayName="Master Bedroom (AD_r)" description="" physical="/.bacnet/22 Quarry Road/200/multi-state-input,100114" />
        <Enumerated name="COMMAND" value="inactive" displayName="Back Bedroom AC Command" description="" physical="/.bacnet/22 Quarry Road/400000/binary-value,800" />
        <Unsigned name="FANCOMMAND" value="6" displayName="Master Bedroom (FS_w)" description="" physical="/.bacnet/22 Quarry Road/200/multi-state-output,100106" />
        <Unsigned name="FANSTATUS" value="5" displayName="Master Bedroom (FS_r)" description="" physical="/.bacnet/22 Quarry Road/200/multi-state-input,100107" />
        <Enumerated name="LOCALOVERRIDE" value="inactive" displayName="Back Bedroom Local Override" description="" physical="/.bacnet/22 Quarry Road/400000/binary-value,57" />
        <Unsigned name="MODECOMMAND" value="5" displayName="Back Bedroom Fan Speed Command" description="" physical="/.bacnet/22 Quarry Road/400000/multi-state-value,800" />
        <Unsigned name="MODESTATUS" value="2" displayName="Master Bedroom (MD_r)" description="" physical="/.bacnet/22 Quarry Road/200/multi-state-input,100105" />
        <Enumerated name="STATUS" value="inactive" displayName="Master Bedroom (ON_r)" description="" physical="/.bacnet/22 Quarry Road/200/binary-input,100102" />
        <Real name="TEMP" value="20.6209" displayName="Back Bedroom Space Temp" description="" physical="/.bacnet/22 Quarry Road/400000/analog-input,208001" />
        <Real name="TEMP_SP" value="20.5" displayName="Back Bedroom Occupied Space Setpoint" description="" physical="/.bacnet/22 Quarry Road/400000/analog-value,800" />
      </Struct>
    </Struct>
    <Struct name="Default System Dashboard" nodeType="Other">
      <Enumerated name="Type" value="System" />
      <Enumerated name="Layout" value="2" />
      <Collection name="Widgets">
        <Struct name="1ae623e1-837f-11e3-b067-94de806b511c">
          <String name="Type" value="Point List" />
        </Struct>
        <Struct name="1ae623ed-837f-11e3-b067-94de806b511c">
          <String name="Type" value="Alarm List" />
        </Struct>
        <Struct name="1ae623fb-837f-11e3-b067-94de806b511c">
          <String name="Type" value="Alarm Summary" />
        </Struct>
      </Collection>
    </Struct>
  </Collection>
  <Collection name="Landing">
    <Struct name="Landing" nodeType="System">
      <String name="location" value="" />
      <String name="address" value="" />
      <String name="description" value="" />
      <String name="template" value="vstat.xml" />
      <Struct name="Objects">
        <Real name="TEMP" value="20.4333" displayName="Landing Space Temp" description="" physical="/.bacnet/22 Quarry Road/400000/analog-input,204001" />
        <Real name="TEMP_SP" value="20.5" displayName="Landing Occupied Space Setpoint" description="" physical="/.bacnet/22 Quarry Road/400000/analog-value,400" />
      </Struct>
    </Struct>
    <Struct name="Default System Dashboard" nodeType="Other">
      <Enumerated name="Type" value="System" />
      <Enumerated name="Layout" value="2" />
      <Collection name="Widgets">
        <Struct name="1ae623e1-837f-11e3-b067-94de806b511c">
          <String name="Type" value="Point List" />
        </Struct>
        <Struct name="1ae623ed-837f-11e3-b067-94de806b511c">
          <String name="Type" value="Alarm List" />
        </Struct>
        <Struct name="1ae623fb-837f-11e3-b067-94de806b511c">
          <String name="Type" value="Alarm Summary" />
        </Struct>
      </Collection>
    </Struct>
  </Collection>
  <Collection name="Hall">
    <Struct name="Hall" nodeType="System">
      <String name="location" value="" />
      <String name="address" value="" />
      <String name="description" value="" />
      <String name="template" value="vstat.xml" />
      <Struct name="Objects">
        <Real name="TEMP" value="20.5518" displayName="Hall Current Space Temp" description="" physical="/.bacnet/22 Quarry Road/400000/analog-value,1105" />
        <Real name="TEMP_SP" value="20.5" displayName="Hall Occupied Space Setpoint" description="" physical="/.bacnet/22 Quarry Road/400000/analog-value,1100" />
      </Struct>
    </Struct>
    <Struct name="Default System Dashboard" nodeType="Other">
      <Enumerated name="Type" value="System" />
      <Enumerated name="Layout" value="2" />
      <Collection name="Widgets">
        <Struct name="1ae623e1-837f-11e3-b067-94de806b511c">
          <String name="Type" value="Point List" />
        </Struct>
        <Struct name="1ae623ed-837f-11e3-b067-94de806b511c">
          <String name="Type" value="Alarm List" />
        </Struct>
        <Struct name="1ae623fb-837f-11e3-b067-94de806b511c">
          <String name="Type" value="Alarm Summary" />
        </Struct>
      </Collection>
    </Struct>
  </Collection>
  <Collection name="Jaxson's Room">
    <Struct name="Jaxson's Room" nodeType="System">
      <String name="location" value="" />
      <String name="address" value="" />
      <String name="description" value="" />
      <String name="template" value="vstat.xml" />
      <Struct name="Objects">
        <Real name="TEMP" value="20.9336" displayName="Small Bedroom Space Temp" description="" physical="/.bacnet/22 Quarry Road/400000/analog-input,203001" />
        <Real name="TEMP_SP" value="20.5" displayName="Small Bedroom Occupied Space Setpoint" description="" physical="/.bacnet/22 Quarry Road/400000/analog-value,300" />
      </Struct>
    </Struct>
    <Struct name="Default System Dashboard" nodeType="Other">
      <Enumerated name="Type" value="System" />
      <Enumerated name="Layout" value="2" />
      <Collection name="Widgets">
        <Struct name="1ae623e1-837f-11e3-b067-94de806b511c">
          <String name="Type" value="Point List" />
        </Struct>
        <Struct name="1ae623ed-837f-11e3-b067-94de806b511c">
          <String name="Type" value="Alarm List" />
        </Struct>
        <Struct name="1ae623fb-837f-11e3-b067-94de806b511c">
          <String name="Type" value="Alarm Summary" />
        </Struct>
      </Collection>
    </Struct>
  </Collection>
  <Collection name="Olivia's Room">
    <Struct name="Olivia's Room" nodeType="System">
      <String name="location" value="" />
      <String name="address" value="" />
      <String name="description" value="" />
      <String name="template" value="vstat.xml" />
      <Struct name="Objects">
        <Real name="TEMP" value="21.0587" displayName="Front Bedroom Space Temp" description="" physical="/.bacnet/22 Quarry Road/400000/analog-input,205001" />
        <Real name="TEMP_SP" value="20.5" displayName="Front Bedroom Occupied Space Setpoint" description="" physical="/.bacnet/22 Quarry Road/400000/analog-value,500" />
      </Struct>
    </Struct>
    <Struct name="Default System Dashboard" nodeType="Other">
      <Enumerated name="Type" value="System" />
      <Enumerated name="Layout" value="2" />
      <Collection name="Widgets">
        <Struct name="1ae623e1-837f-11e3-b067-94de806b511c">
          <String name="Type" value="Point List" />
        </Struct>
        <Struct name="1ae623ed-837f-11e3-b067-94de806b511c">
          <String name="Type" value="Alarm List" />
        </Struct>
        <Struct name="1ae623fb-837f-11e3-b067-94de806b511c">
          <String name="Type" value="Alarm Summary" />
        </Struct>
      </Collection>
    </Struct>
  </Collection>
  <Collection name="Tyler's Room">
    <Struct name="Tyler's Room" nodeType="System">
      <String name="location" value="" />
      <String name="address" value="" />
      <String name="description" value="" />
      <String name="template" value="vstat.xml" />
      <Struct name="Objects">
        <Real name="TEMP" value="20.3674" displayName="Extension Bedroom Space Temp" description="" physical="/.bacnet/22 Quarry Road/400000/analog-input,202001" />
        <Real name="TEMP_SP" value="20.5" displayName="Extension Bedroom Occupied Space Setpoint" description="" physical="/.bacnet/22 Quarry Road/400000/analog-value,200" />
      </Struct>
    </Struct>
    <Struct name="Default System Dashboard" nodeType="Other">
      <Enumerated name="Type" value="System" />
      <Enumerated name="Layout" value="2" />
      <Collection name="Widgets">
        <Struct name="1ae623e1-837f-11e3-b067-94de806b511c">
          <String name="Type" value="Point List" />
        </Struct>
        <Struct name="1ae623ed-837f-11e3-b067-94de806b511c">
          <String name="Type" value="Alarm List" />
        </Struct>
        <Struct name="1ae623fb-837f-11e3-b067-94de806b511c">
          <String name="Type" value="Alarm Summary" />
        </Struct>
      </Collection>
    </Struct>
  </Collection>
  <Collection name="Kitchen">
    <Struct name="Kitchen" nodeType="System">
      <String name="location" value="" />
      <String name="address" value="" />
      <String name="description" value="" />
      <String name="template" value="vstat.xml" />
      <Struct name="Objects">
        <Real name="TEMP" value="20.2457" displayName="Kitchen Space Temp" description="" physical="/.bacnet/22 Quarry Road/400000/analog-input,210001" />
        <Real name="TEMP_SP" value="20.5" displayName="Kitchen Occupied Space Setpoint" description="" physical="/.bacnet/22 Quarry Road/400000/analog-value,1000" />
      </Struct>
    </Struct>
    <Struct name="Default System Dashboard" nodeType="Other">
      <Enumerated name="Type" value="System" />
      <Enumerated name="Layout" value="2" />
      <Collection name="Widgets">
        <Struct name="1ae623e1-837f-11e3-b067-94de806b511c">
          <String name="Type" value="Point List" />
        </Struct>
        <Struct name="1ae623ed-837f-11e3-b067-94de806b511c">
          <String name="Type" value="Alarm List" />
        </Struct>
        <Struct name="1ae623fb-837f-11e3-b067-94de806b511c">
          <String name="Type" value="Alarm Summary" />
        </Struct>
      </Collection>
    </Struct>
  </Collection>
  <Collection name="Dining Room">
    <Struct name="Dining Room" nodeType="System">
      <String name="location" value="" />
      <String name="address" value="" />
      <String name="description" value="" />
      <String name="template" value="vstat.xml" />
      <Struct name="Objects">
        <Real name="TEMP" value="19.8079" displayName="Dining Room Space Temp" description="" physical="/.bacnet/22 Quarry Road/400000/analog-input,207001" />
        <Real name="TEMP_SP" value="20.5" displayName="Dining Room Occupied Space Setpoint" description="" physical="/.bacnet/22 Quarry Road/400000/analog-value,700" />
      </Struct>
    </Struct>
    <Struct name="Default System Dashboard" nodeType="Other">
      <Enumerated name="Type" value="System" />
      <Enumerated name="Layout" value="2" />
      <Collection name="Widgets">
        <Struct name="1ae623e1-837f-11e3-b067-94de806b511c">
          <String name="Type" value="Point List" />
        </Struct>
        <Struct name="1ae623ed-837f-11e3-b067-94de806b511c">
          <String name="Type" value="Alarm List" />
        </Struct>
        <Struct name="1ae623fb-837f-11e3-b067-94de806b511c">
          <String name="Type" value="Alarm Summary" />
        </Struct>
      </Collection>
    </Struct>
  </Collection>
  <Collection name="Living Room">
    <Struct name="Living Room" nodeType="System">
      <String name="location" value="" />
      <String name="address" value="" />
      <String name="description" value="" />
      <String name="template" value="vstat.xml" />
      <Struct name="Objects">
        <Enumerated name="Heating Override" value="inactive" displayName="Living Room Local Override" description="" physical="/.bacnet/22 Quarry Road/400000/binary-value,55" />
        <Real name="TEMP" value="21.0587" displayName="Living Room Space Temp" description="" physical="/.bacnet/22 Quarry Road/400000/analog-input,206001" />
        <Real name="TEMP_SP" value="20.5" displayName="Living Room Occupied Space Setpoint" description="" physical="/.bacnet/22 Quarry Road/400000/analog-value,600" />
      </Struct>
    </Struct>
    <Struct name="Default System Dashboard" nodeType="Other">
      <Enumerated name="Type" value="System" />
      <Enumerated name="Layout" value="2" />
      <Collection name="Widgets">
        <Struct name="1ae623e1-837f-11e3-b067-94de806b511c">
          <String name="Type" value="Point List" />
        </Struct>
        <Struct name="1ae623ed-837f-11e3-b067-94de806b511c">
          <String name="Type" value="Alarm List" />
        </Struct>
        <Struct name="1ae623fb-837f-11e3-b067-94de806b511c">
          <String name="Type" value="Alarm Summary" />
        </Struct>
      </Collection>
    </Struct>
  </Collection>
</Collection>}

Open in new window

LVL 1
wint100Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
You need a XSD schema. When one exists, then use ths otherwise use the XSD.exe tool to create one.
Then you use the XSD.exe again to create the necessary classes for deserialization.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
wint100Author Commented:
I've tried doing this but the XML changes too much. I'll try it in chunks and see how it goes.

Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
XML

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.