简体   繁体   English

使用C#读取XML文件

[英]Reading in XML file with C#

I am having trouble reading in the XML file with my C# program. 我在使用C#程序读取XML文件时遇到问题。 When i try to run it I get an error saying that "An unhandled exception of type 'System.Xml.XPath.XPathException' occurred in System.Xml.dll 当我尝试运行它时,我收到一条错误消息,提示“ System.Xml.dll中发生了'System.Xml.XPath.XPathException类型的未处理的异常”

Additional information: Expression must evaluate to a node-set." 附加信息:表达式必须评估为节点集。“

XML Code: 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# Code: 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);
}                

All you need is 所有你需要的是

foreach (XmlNode node in xDoc.SelectNodes("musicstore/album"))

The problem is with the last / . 问题出在最后一个/

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

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