Hello All,
I'm adding functionality to one of our existing (but broken) systems. It grabs an XML file from a web service, parses it and then does some stuff before packing it back into our database.
the previous developer (who has now left) left me this little gem:
http://dl.getdropbox.com/u/109069/wut.GIF
and I wonder if there's a way round this?
Can I loop through each node and assign to the wo object by its name?
something like this (pseudo code):
foreach XmlNode xn in WorkorderNodeTree
{
//find out the property name of the current node
//match to the property in the workorder class
//set the value equal
wo.<xn.name> = xn.innertext
}
Now the only thing I found which gets close is this (from the interweb):
foreach (XmlNode xl in myXML)
{
object o = Assembly.GetExecutingAssembly().CreateInstance("Workorder", true);
Type t = xl.Name.GetType();
PropertyInfo pi = t.GetProperty(xl.Name);
pi.SetValue(o, xl.InnerText, null);
}
but it returns a null reference exception on o. I am a little confused, any tips?
I presume to do this, I need to use reflection or generics, but I've never hit upon these things before - can anyone advise anything which might point me in the right direction or at least try to explain reflection?
Many thanks all, apologies for the hideously long post!
EDIT:
Thanks, Very deep and sincere thanks go to Fredrik and Rytmis - both of you are white knights in my drab office environment. Rytmis' code edits have solved the issue but I have learned much in this hour or so - Thanks guys, really appreciate it.