Reputation: 10137
public void AssignSkillFromXml(string xmlCharID, string skillNodeDir)
{
XmlDocument doc = new XmlDocument();
doc.Load(@"/home/holland/code/svn/chronos-/trunk/chronos/Lib/XmlFiles/Characters.xml");
XmlNode node = doc.SelectSingleNode("Character/"+xmlCharID+"/Skills/"+skillNodeDir+"/text()");
foreach(KeyValuePair<Skill, int> entry in skills)
{
bool nodeEqualsKey =
(node.Name.ToString() == entry.Key.ToString());
Console.WriteLine(entry.Key);
if (nodeEqualsKey)
{
int val;
int.TryParse(node.Value, out val);
skills[entry.Key] = val;
Console.WriteLine(val);
}
}
}
As shown, I have a method which is designed to assign a value from an xml node. The name of the node is supposed to be passed as a string, and accessed through the directory specified by the XmlNode.
From there, a foreach loop iterates through a dictionary, comparing the name of the node with the key of the dictionary, which happens to be an enum. The only way this will work is if the enum can be parsed as a string, and compared with the name of the node. I would like to just use the skillNodeDir to compare the skill as such with the enum, but this isn't very safe, and is more subject to human error.
Is there an alternative?
Upvotes: 0
Views: 225
Reputation: 244757
Your code doesn't make much sense, you're using Name
of a text node, which is always #text
.
I'm not sure this is what you are looking for, but you can use Enum.Parse()
, to get the enum value from the string. Normal directory indexing should then work with that.
Upvotes: 1