[英]Get a the value of a node in an XML file in C#?
我一直在嘗試閱讀給定菜單項的卡路里,但它不起作用。 這是我的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");
因為它不知道哪個項目。 那么,如何告訴它獲取名稱為“漢堡”的卡路里?
//項[名= '漢堡'] /卡路里
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.