2

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); } 
1
  • 4
    Remove the last / on musicstore/album/ Commented Aug 17, 2013 at 21:53

1 Answer 1

7

All you need is

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

The problem is with the last /.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.