简体   繁体   中英

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.

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 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?

Thank you in advance.

Use InnerText instead of Value

Replace

string checker = node.Value;

With

string checker = node.InnerText;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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