miceiken
miceiken

Reputation: 21

Trouble using XPath

I am trying to parse the Spotify lookup register, but after several attempts I am trying here as a last resort.

Related XML: http://ws.spotify.com/lookup/1/?uri=http://open.spotify.com/track/3aKJVWH2QOsaMtiCLxYHZX

Related code:

var response = streamReader.ReadToEnd();
xml.LoadXml(response);
var name = xml.SelectSingleNode("//track/name").InnerText;
var artist = xml.SelectSingleNode("//track/artist/name").InnerText;
var album = xml.SelectSingleNode("//track/album/name").InnerText;
var length = xml.SelectSingleNode("//track/length").InnerText;

Related debugging: http://screencast.com/t/yDc8f7Wyzv9P

Upvotes: 0

Views: 226

Answers (5)

miceiken
miceiken

Reputation: 21

Decided to try another approach, thanks to Windcape I found XmlSerializer

var serializer = new XmlSerializer(typeof(Track));
var track = (Track)serializer.Deserialize(streamReader);

var time = TimeSpan.FromSeconds(track.length);

var ircResponse = string.Format("{0} - {1} ({2}) | {3}min {4}sec | {5}% popular",
  track.artist.name,
  track.name,
  track.album.name,
  time.TotalMinutes, time.Seconds,
  Math.Round(track.popularity * 100, 0));

CommandHandler.Msg(chan, ircResponse);

Upvotes: 0

Peter
Peter

Reputation: 48958

My guess is that you should prefix your XPath expression, or ommit the default namespace :

http://msdn.microsoft.com/en-us/library/h0hw012b.aspx#Y1372

states :

Note If the XPath expression does not include a prefix, it is assumed that the namespace URI is the empty namespace. If your XML includes a default namespace, you must still add a prefix and namespace URI to the XmlNamespaceManager; otherwise, you will not get a node selected. For more information, see Select Nodes Using XPath Navigation.

following your example :

 XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
 nsmgr.AddNamespace("ns", "http://http://www.spotify.com/ns/music/1");
 var name = xml.SelectSingleNode("//ns:track/ns:name", nsmgr);

Upvotes: 3

Brent Stewart
Brent Stewart

Reputation: 1840

Why not use LINQ to XML and make your life easier?

Upvotes: 0

user357812
user357812

Reputation:

This is a FAQ: You need to register the namespace an use it in the XPath expression

XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("ns", "http://www.spotify.com/ns/music/1");
var name = xml.SelectSingleNode("/ns:track/ns:name", nsmgr); 

Also note that there is no need for a starting // operator when th schema is well known.

Upvotes: 1

Anomie
Anomie

Reputation: 94794

Quoth the documentation:

If the XPath expression does not include a prefix, it is assumed that the namespace URI is the empty namespace. If your XML includes a default namespace, you must still add a prefix and namespace URI to the XmlNamespaceManager; otherwise, you will not get any nodes selected.

The XML document does have a default namespace, specifically http://www.spotify.com/ns/music/1. You'll need to supply a prefix and namespace URI for your xpath query.

Upvotes: 0

Related Questions