Avatar of wint100
wint100
Flag for United Kingdom of Great Britain and Northern Ireland asked on

Deserialize XML to C#

I'm trying to deserialize an XML reponse into a C# class but due t the complexity of the XML and the re-use of names, Restsharp only gets the top level elements:

The elements I'm ooking to get are the Collection types where nodeType="System", I need a List of all of these that include the Objects for each collection and also the 'Real' objects that are within the Objects elements.

So, the result would be a list of Collections (e.g. Bedroom, Kitchen). Each item would have an Objects Collection, that contained a list of Objects (e.g. "TEMP_SP", "TEMP")

I'm using Restsharp but need the Class structure to handle full deserialization of the XML response.

<?xml version="1.0" encoding="UTF-8"?>

-<Collection xmlns="http://bacnet.org/csml/1.2" name="Test">


-<Collection name="Bedroom">


-<Struct name="Bedroom" 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_SP" value="23" physical="/.bacnet/Birchall HQ/1300/analog-value,10" description="" displayName="GB Occupied Space Setpoint"/>

<Real name="TEMP" value="15.8905" physical="/.bacnet/Birchall HQ/1300/analog-input,2" description="" displayName="Conference Room Temp"/>

</Struct>

</Struct>


-<Struct name="Default System Dashboard" nodeType="Other">

<Enumerated name="Type" value="System"/>

<Enumerated name="Layout" value="2"/>


-<Collection name="Widgets">


-<Struct name="763631f4-f701-11e3-8d86-d4bed9a91498">

<String name="Type" value="Alarm Summary"/>

</Struct>


-<Struct name="764769bc-f701-11e3-8d86-d4bed9a91498">

<String name="Type" value="Point List"/>

</Struct>


-<Struct name="764769d6-f701-11e3-8d86-d4bed9a91498">

<String name="Type" value="Alarm List"/>

</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_SP" value="23" physical="/.bacnet/Birchall HQ/1300/analog-value,10" description="" displayName="GB Occupied Space Setpoint"/>

<Real name="TEMP" value="15.8905" physical="/.bacnet/Birchall HQ/1300/analog-input,2" description="" displayName="Conference Room Temp"/>

</Struct>


-<Struct name="Default System Dashboard" nodeType="Other">

<Enumerated name="Type" value="System"/>

<Enumerated name="Layout" value="2"/>


-<Collection name="Widgets">


-<Struct name="763631f4-f701-11e3-8d86-d4bed9a91498">

<String name="Type" value="Alarm Summary"/>

</Struct>


-<Struct name="764769bc-f701-11e3-8d86-d4bed9a91498">

<String name="Type" value="Point List"/>

</Struct>


-<Struct name="764769d6-f701-11e3-8d86-d4bed9a91498">

<String name="Type" value="Alarm List"/>

</Struct>

</Collection>

</Struct>

</Collection>

</Collection>

Open in new window

.NET ProgrammingC#XML

Avatar of undefined
Last Comment
wint100

8/22/2022 - Mon
ste5an

I would start by using the XSD utility to generate the destination classes. But sometimes you simply need to manully (re-)define those classes.

p.s. you XML is invalid.
wint100

ASKER
I tried using the ‘Paste XML to Class’ function in VS2017 but that only got the root element. I’ll have a look at the tool you suggested. Thanks

(I did manually add in text to the Objects section so I might have made it invalid)
wint100

ASKER
<?xml version="1.0" encoding="UTF-8"?>
<Collection name="Test" xmlns="http://bacnet.org/csml/1.2"><Collection name="Bedroom"><Struct name="Bedroom" 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_SP" value="23" displayName="GB Occupied Space Setpoint" description="" physical="/.bacnet/Birchall HQ/1300/analog-value,10"/><Real name="TEMP" value="15.8905" displayName="Conference Room Temp" description="" physical="/.bacnet/Birchall HQ/1300/analog-input,2"/></Struct></Struct><Struct name="Default System Dashboard" nodeType="Other"><Enumerated name="Type" value="System"/><Enumerated name="Layout" value="2"/><Collection name="Widgets"><Struct name="763631f4-f701-11e3-8d86-d4bed9a91498"><String name="Type" value="Alarm Summary"/></Struct><Struct name="764769bc-f701-11e3-8d86-d4bed9a91498"><String name="Type" value="Point List"/></Struct><Struct name="764769d6-f701-11e3-8d86-d4bed9a91498"><String name="Type" value="Alarm List"/></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"/></Struct><Struct name="Default System Dashboard" nodeType="Other"><Enumerated name="Type" value="System"/><Enumerated name="Layout" value="2"/><Collection name="Widgets"><Struct name="763631f4-f701-11e3-8d86-d4bed9a91498"><String name="Type" value="Alarm Summary"/></Struct><Struct name="764769bc-f701-11e3-8d86-d4bed9a91498"><String name="Type" value="Point List"/></Struct><Struct name="764769d6-f701-11e3-8d86-d4bed9a91498"><String name="Type" value="Alarm List"/></Struct></Collection></Struct></Collection></Collection>

Open in new window

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Jose Gabriel Ortega Castro

Basically, the exercise would be to create a temp text file with the correct use of XML, that is a valid XML but is not valid in the way the data is applied inside it. you'd need to change the Most External "Collection" to be something like "Collections" that have a Collection inside.
wint100

ASKER
Pasting XML as Classes in VS2017 did the trick. I was hten able to parse the XML as an object
ASKER CERTIFIED SOLUTION
wint100

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.