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.:
< lp >10 lp >
thanks for ur help :)
Phorex
Phorex hPosted Nov 27, 2011, 2:23 PM
Player p = new Player(0,0,0,0,false, "", "");
foreach(PropertyInfo pi in p.GetType().GetProperties())
{
foreach( XmlNode item in node.ChildNodes )
{
if( item.Name == pi.Name )
{
pi.GetSetMethod().Invoke( p, new object[] { item.InnerText } ); }
}
}
excuse me, i'm just workin now for about 15 hours and i'm a "bit" tired :D
so my question is where to get Information about how to cast the item.InnerText to the expected value (so no hand written setter is needed)?
Sam HobbsPosted Nov 27, 2011, 2:07 PM
Note that "auto-update the xml files, when the source-code changes. so old values are kept and new ones get inserted" is ambiguous to me; I am not sure if you mean data or data items. So in other words, you might mean that if you have a field abc then the XML file might have abc=99 and you want to update the file with another abc with the value of 88. You probably however mean that you want to be able to add a field such as a bcd field and you want to automatically update your source code to also process the bcd field, but you say "auto-update the xml files" when you want to auto-update the source code or at least the fields that are processed during execution. But can you understand that it is confusing?
Phorex hPosted Nov 27, 2011, 1:53 PM
like this is the saving method:
private XmlNode SerializePlayerToNode( Player p, XmlDocument doc )
{
XmlNode node = doc.CreateElement( p.Name );
if( p != null && doc != null )
foreach( PropertyInfo pi in p.GetType().GetProperties() )
{
XmlNode tmp = doc.CreateElement( pi.Name );
tmp.InnerText = pi.GetGetMethod().Invoke( p, null ).ToString();
node.AppendChild( tmp );
}
return node;
}
and now i need one to load dynamically a class with values, stored in the xml file
Sam HobbsPosted Nov 27, 2011, 1:49 PM