Phorex h

Phorex h

  • NA
  • 21
  • 10.2k

XML-Reflection problem

Nov 27 2011 12:36 PM
hey guys,

i need to know, how to dynamically associate certain values stored in an xml file with values used in an object.

for e.g.:

my readmethod (loop runs through the document and calls on certain nodes the readmethod) 
 
private NPC ReadNPC( XmlNode node )
{
//the next 5 lines have to be replaced...
bool unique = bool.Parse( node["isUnique"].InnerText );
string name = node.Name;
int lp = int.Parse( node["LP"].InnerText );
int atk = int.Parse( node["ATK"].InnerText );
string description = node["Description"].InnerText;
return new NPC( lp, atk, unique, name, description );
}
 

and here's my NPC class
public class NPC
{
private bool unique;
private string name;
private int lp;
private int atk;
private string description;

  protected  NPC ( int lp, int atk, bool isUnique, string name, string descr )
{
this.lp = lp;
this.atk = atk;
this.name = name;
this.description = descr;
this.unique = isUnique;
}
}

now i want to replace all the un!-dynamic lines reading the xmlnode with some reflective code, managing automatic value setting.
i've already written methods, writing xml files with reflection, so changes/updates in these classes (like NPC), will not cause any greater errors, when it comes to save 'em (but i have to admit, that i'm not very familiar with .NET- Reflection).
 
btw: the saving methods uses the names of the properties of an NPC-Instance as xmlnode-name.
for e.g.:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<NPCs>
<stdNPC>
  <unique>False</ unique >
  <name>stdNPC</ name >
  <description>text</ description >
< lp >10</ lp >
  <atk>1</atk>
</stdNPC>
</NPCs>

thanks for ur help :)
Phorex 

Answers (4)