开发者

Obtain string representation of XML node

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 t开发者_运维知识库o 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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜