简体   繁体   English

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

[英]XML select single node not returning anything

I have the following method that is supposed to return a string that holds the calories for a given food item in an xml menu. 我有以下方法,该方法应该返回一个字符串,该字符串保存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;
    }

And my XML file looks like this: 我的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>

Why is it returning an empty string? 为什么返回一个空字符串? Is my call to SelectSingleNode incorrect? 我对SelectSingleNode呼叫不正确吗?

Thank you in advance. 先感谢您。

Use InnerText instead of Value 使用InnerText代替Value

Replace 更换

string checker = node.Value;

With

string checker = node.InnerText;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM