FJam
FJam

Reputation: 756

Reading in XML file with C#

I am having trouble reading in the XML file with my C# program. 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

Additional information: Expression must evaluate to a node-set."

XML Code:

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

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);
}                

Upvotes: 2

Views: 944

Answers (1)

I4V
I4V

Reputation: 35363

All you need is

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

The problem is with the last /.

Upvotes: 7

Related Questions