我在使用 C# 程序读取 XML 文件时遇到问题。当我尝试运行它时,我收到一条错误消息 “System.Xml.dll 中发生类型为‘System.Xml.XPath.XPathException’的未处理异常
附加信息:表达式必须求值为节点集。”
XML 代码:
<musicstore>
<album>
<name>Black Album</name>
<artist>Metallica</artist>
<year>1991</year>
<price>$10.00</price>
</album>
<album>
<name>Exodus</name>
<artist>Bob Marley</artist>
<year>1979</year>
<price>$5.99</price>
</album>
</musicstore>
C#代码:
XmlDocument xDoc = new XmlDocument();
xDoc.Load("C:\\Users\\FJam\\Desktop\\Coding\\XML\\text.xml");
foreach(XmlNode node in xDoc.SelectNodes("musicstore/album/"))
{
MessageBox.Show(node.SelectSingleNode("artist").InnerText);
}
最佳答案
你只需要
foreach (XmlNode node in xDoc.SelectNodes("musicstore/album"))
问题出在最后的/
。
关于c# - C#读取XML文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18293606/