gpt4 book ai didi

c# - C#读取XML文件

转载 作者:数据小太阳 更新时间:2023-10-29 02:51:30 26 4
gpt4 key购买 nike

我在使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com