I am trying to deserialize XML response received from moodle web services.
I could parse it into a dotnet object if it had distinct named attributes like id, shortname, idnumber etc. But it has got an array of KEY attributes with actual field name as a value and inside it, there is another node having the field value.
Here is a sample:
<?xml version="1.0" encoding="UTF-8" ?>
<RESPONSE>
<MULTIPLE>
<SINGLE>
<KEY name="id">
<VALUE>2</VALUE>
</KEY>
<KEY name="shortname">
<VALUE>CS-101</VALUE>
</KEY>
<KEY name="fullname">
<VALUE>CS-101</VALUE>
</KEY>
<KEY name="enrolledusercount">
<VALUE>2</VALUE>
</KEY>
<KEY name="idnumber">
<VALUE></VALUE>
</KEY>
<KEY name="visible">
<VALUE>1</VALUE>
</KEY>
<KEY name="summary">
<VALUE><p>CS-101<br /></p></VALUE>
</KEY>
<KEY name="summaryformat">
<VALUE>1</VALUE>
</KEY>
<KEY name="format">
<VALUE>weeks</VALUE>
</KEY>
<KEY name="showgrades">
<VALUE>1</VALUE>
</KEY>
<KEY name="lang">
<VALUE></VALUE>
</KEY>
<KEY name="enablecompletion">
<VALUE>0</VALUE>
</KEY>
</SINGLE>
</MULTIPLE>
</RESPONSE>
I want to parse this XML into an object of this class:
class Course
{
public int id { get; set; }
public string shortname { get; set; } //short name of course
public string fullname { get; set; } //long name of course
public int enrolledusercount { get; set; } //Number of enrolled users in this course
public string idnumber { get; set; } //id number of course
public int visible { get; set; } //1 means visible, 0 means hidden course
public string summary { get; set; }
public int summaryformat { get; set; } //summary format (1 = HTML, 0 = MOODLE, 2 = PLAIN or 4 = MARKDOWN)
public string format { get; set; } //course format: weeks, topics, social, site
public int showgrades { get; set; } //true if grades are shown, otherwise false
public string lang { get; set; } //forced course language
public int enablecompletion { get; set; } //true if completion is enabled, otherwise false
}
Is there a direct way to do it or should I write a parser method with switch cases for each field?