我有以下方法应该返回一个字符串,该字符串保存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
的电话是否错误?
提前谢谢。
答案 0 :(得分:1)
使用InnerText
代替Value
替换
string checker = node.Value;
用
string checker = node.InnerText;