0
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?

0

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.