XML选择单个节点不返回任何内容

时间:2013-10-24 19:14:01

标签: c# xml

我有以下方法应该返回一个字符串,该字符串保存xml菜单中给定食物的卡路里。

public string calorieCount(int choice)
    {
        string calCount = "250";
        XmlDocument doc = new XmlDocument();
        doc.Load(path);
        XmlElement root = doc.DocumentElement;
        XmlNode node = doc.SelectSingleNode("/menu/item[@name='Burger']/calories");
        string checker = node.Value;
        MessageBox.Show(checker);//returning nothing
        return checker;
    }

我的XML文件如下所示:

<?xml version="1.0" encoding="utf-8" ?>
<menu>
  <!-- Burger -->
  <item name="Burger">
    <name>Burger</name>
    <price>$5.99</price>
    <calories>500</calories>
    <description>A burger made with 100% angus beef and grilled to your liking. Served     with fries</description>
    <count>25</count>
  </item>

为什么返回一个空字符串?我对SelectSingleNode的电话是否错误?

提前谢谢。

1 个答案:

答案 0 :(得分:1)

使用InnerText代替Value

替换

string checker = node.Value;

string checker = node.InnerText;