特定のメニュー項目のカロリーを読み取ろうとしてきましたが、機能していません。私の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");
どのアイテムか分からないので、問題があると思います。では、「バーガー」という名前のアイテムのカロリーを取得するにはどうすればよいでしょうか。