jeudi 4 septembre 2014

XML Deserialization with dynamic fields


Vote count:

0




Say I have the following xml:



<rootelement>
<childone>val1</childone>
<childtwo>val2</childtwo>
<childthree>val3</childthree>
</rootelement>


To deserialize it into an object I'd normally have something like this:



public class rootelement{
public string childone,childtwo,childthree;
}


That all works, but now I want to find out if I can have the child node names stored in an array or something so I can manage my fields more easily, and use this array to populate the keys in a List of KeyValuePair eg:



string[] fieldnames={"childone","childtwo","childthree"};
List<KeyValuePair<string, string>> fields=new List<KeyValuePair<string, string>>();
for(int i=0;i<fieldnames.Length;i++){
fields.Add(new KeyValuePair<string,string>(fieldnames[i],""));
}


And the last step would be for deserialization to populate the values. It doesn't have to be a List of KeyValuePair specifically, anything that I could apply the same concept to would work. Is something like this possible? If so, please assist me with an example.


Thanks in advance.



asked 2 mins ago







XML Deserialization with dynamic fields

Aucun commentaire:

Enregistrer un commentaire