我一直试图读取给定菜单项的卡路里,但它无效。这是我的XML文件的样子。
<?xml version="1.0" encoding="utf-8" ?>
<menu>
<!-- Burger -->
<item>
<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>
</menu>
我尝试读取卡路里的功能看起来像这样
public string calorieCount(int choice)
{
string calCount="";
string path = "XMLFile1.xml";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(path);
XmlElement root = xmlDoc.DocumentElement;
switch (choice)
{
case '0':
//read the calories of burger and fries and return
var node = root.SelectSingleNode("//item/name/calories");
calCount = node.Value;
break;
}
return calCount;
}
我认为问题出在var node = root.SelectSingleNode("//item/name/calories");
,因为它不知道哪个项目。那么如何告诉它获取名为“Burger”的物品的卡路里?
答案 0 :(得分:2)
//项[名= '汉堡'] /卡路里