1

我一直在尝试读取给定菜单项的卡路里,但它不起作用。这是我的 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”的项目的卡路里?

4

1 回答 1

2

//item[name='Burger']/卡路里

于 2013-10-22T22:25:51.827 回答