Suppose I am trying to deserialize the following XML (in a C# program):
<Data>
<DataItem Id="1" Attrib1="Val1_1" Attrib2="Val2_1" Attrib3="Val3_1" />
<DataItem Id="2" Attrib1="Val1_2" Attrib4="Val4_2" />
<DataItem Id="3" Attrib3="Val3_1" Attrib5="Val5_3" />
</Data>
So, each DataItem
element will have an Id
field and a random set of attributes I'd like to capture / store.
I'm imagining the final containing classes could look as follows:
[XmlRootAttribute("Data")]
public class Data
{
[XmlElement("DataItem")]
public DataItem[] Items { get; set; }
}
public class DataItem
{
public Dictionary<string, object> Vals { get; set; }
[XmlIgnore]
public string Id { get { return (string)Vals[Id]; } }
}
(If I'm wrong on this structure, PLEASE let me know - this is still all very new to me!!)
But I'm not sure how to deserialize the attributes into the [Key][Value]
pairs for my dictionary.
I found this question and also this other answer that seem to be pointing me in the right (albeit different) directions, but I'm lost in how to implement this correctly and know this should be fairly easy.
Any help / links / samples on how this could be done would be GREATLY appreciated!!
Thanks!!!