NostraThomas NostraThomas - 2 days ago 3
C# Question

XmlDocument.SelectNodes not finding notes (C#)

I'm having trouble parsing an XML document with XmlDocument.SelectNodes. The XML document I'm trying to parse is below:

<ArrayOfSearchLyricResult xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://api.chartlyrics.com/">
<SearchLyricResult>
<TrackId>0</TrackId>
<LyricChecksum>2d9e0cae0445e5e483edfe140d89c9f3</LyricChecksum>
<LyricId>103</LyricId>
<SongUrl>
http://www.chartlyrics.com/DhNJp9jCfEq3QJCOpFdJYQ/You+Sexy+Thing.aspx </SongUrl>
<ArtistUrl>
http://www.chartlyrics.com/DhNJp9jCfEq3QJCOpFdJYQ.aspx
</ArtistUrl>
<Artist>Hot Chocolate</Artist>
<Song>You Sexy Thing</Song>
<SongRank>9</SongRank>
</SearchLyricResult>
<SearchLyricResult>
<TrackId>0</TrackId>
<LyricChecksum>8101c7b585e1a4bf0703604e21203b08</LyricChecksum>
<LyricId>299</LyricId>
<SongUrl>
http://www.chartlyrics.com/BDhOYTR9ukGFf4EbrXwreg/Sultans+of+Swing.aspx
</SongUrl>
<ArtistUrl>
http://www.chartlyrics.com/BDhOYTR9ukGFf4EbrXwreg.aspx
</ArtistUrl>
<Artist>Dire Straits</Artist>
<Song>Sultans of Swing</Song>
<SongRank>9</SongRank>
</SearchLyricResult>
<SearchLyricResult>
<TrackId>0</TrackId>
<LyricChecksum>7ca62310384a876bfd77cb5973eea84f</LyricChecksum>
<LyricId>476</LyricId>
<SongUrl>
http://www.chartlyrics.com/bTsqWSvEZ0WZyez2O9ZkmQ/Johnny+B.+Goode.aspx
</SongUrl>
<ArtistUrl>
http://www.chartlyrics.com/bTsqWSvEZ0WZyez2O9ZkmQ.aspx
</ArtistUrl>
<Artist>Chuck Berry</Artist>
<Song>Johnny B. Goode</Song>
<SongRank>9</SongRank>
</SearchLyricResult>
<SearchLyricResult>
<TrackId>0</TrackId>
<LyricChecksum>2db7cbd379f393f1cc2db14b0e090890</LyricChecksum>
<LyricId>331</LyricId>
<SongUrl>
http://www.chartlyrics.com/dSNGSwjFKkOMiM7uw4sWrg/Kids+in+America.aspx
</SongUrl>
<ArtistUrl>
http://www.chartlyrics.com/dSNGSwjFKkOMiM7uw4sWrg.aspx
</ArtistUrl>
<Artist>Kim Wilde</Artist>
<Song>Kids in America</Song>
<SongRank>9</SongRank>
</SearchLyricResult>
<SearchLyricResult>
<TrackId>0</TrackId>
<LyricChecksum>fdfe0c7fa58cc330f5f65c07d51bc223</LyricChecksum>
<LyricId>290</LyricId>
<SongUrl>
http://www.chartlyrics.com/jFWSj6orWEeMOGFVGene2g/London+Calling.aspx
</SongUrl>
<ArtistUrl>
http://www.chartlyrics.com/jFWSj6orWEeMOGFVGene2g.aspx
</ArtistUrl>
<Artist>The Clash</Artist>
<Song>London Calling</Song>
<SongRank>9</SongRank>
</SearchLyricResult>
<SearchLyricResult>
<TrackId>0</TrackId>
<LyricChecksum>08ae7d63ce44d9c844e012df821cc8dd</LyricChecksum>
<LyricId>518</LyricId>
<SongUrl>
http://www.chartlyrics.com/_fHX8YOI6UetK-nsn_YarA/Soul+Man.aspx
</SongUrl>
<ArtistUrl>
http://www.chartlyrics.com/_fHX8YOI6UetK-nsn_YarA.aspx
</ArtistUrl>
<Artist>Sam & Dave</Artist>
<Song>Soul Man</Song>
<SongRank>9</SongRank>
</SearchLyricResult>
<SearchLyricResult>
<TrackId>0</TrackId>
<LyricChecksum>afd610c96ee6fed3a1339d4bb0ff03a2</LyricChecksum>
<LyricId>1149</LyricId>
<SongUrl>
http://www.chartlyrics.com/rIJkhMTUhkKAArFKYB2OwA/Car+Wash.aspx
</SongUrl>
<ArtistUrl>
http://www.chartlyrics.com/rIJkhMTUhkKAArFKYB2OwA.aspx
</ArtistUrl>
<Artist>Rose Royce</Artist>
<Song>Car Wash</Song>
<SongRank>9</SongRank>
</SearchLyricResult>
<SearchLyricResult>
<TrackId>0</TrackId>
<LyricChecksum>9b26935781b09651067d568879d16fb1</LyricChecksum>
<LyricId>342</LyricId>
<SongUrl>
http://www.chartlyrics.com/soiNZ7CHO0C2PV2nRlrsww/Immigrant+Song.aspx
</SongUrl>
<ArtistUrl>
http://www.chartlyrics.com/soiNZ7CHO0C2PV2nRlrsww.aspx
</ArtistUrl>
<Artist>Led Zeppelin</Artist>
<Song>Immigrant Song</Song>
<SongRank>9</SongRank>
</SearchLyricResult>
<SearchLyricResult>
<TrackId>0</TrackId>
<LyricChecksum>7640651d7b769712fcd7fa7240b71bb6</LyricChecksum>
<LyricId>163</LyricId>
<SongUrl>
http://www.chartlyrics.com/vYsnIBonHEq2_UOhPUMgPQ/Video+Killed+the+Radio+Star.aspx
</SongUrl>
<ArtistUrl>
http://www.chartlyrics.com/vYsnIBonHEq2_UOhPUMgPQ.aspx
</ArtistUrl>
<Artist>The Buggles</Artist>
<Song>Video Killed the Radio Star</Song>
<SongRank>9</SongRank>
</SearchLyricResult>
<SearchLyricResult>
<TrackId>0</TrackId>
<LyricChecksum>9266af12dd9157562b7c707b53f3bdc3</LyricChecksum>
<LyricId>4342</LyricId>
<SongUrl>
http://www.chartlyrics.com/LXMjzdL_TkSIhFNHXXrH2Q/The+Power.aspx
</SongUrl>
<ArtistUrl>
http://www.chartlyrics.com/LXMjzdL_TkSIhFNHXXrH2Q.aspx
</ArtistUrl>
<Artist>Snap!</Artist>
<Song>The Power</Song>
<SongRank>9</SongRank>
</SearchLyricResult>
<SearchLyricResult>
<TrackId>0</TrackId>
<LyricChecksum>3986a113157b0ac33d0aed5656fd9f06</LyricChecksum>
<LyricId>940</LyricId>
<SongUrl>
http://www.chartlyrics.com/cNVr9GhXLka4TMfJk7v0fg/Life+in+the+Fast+Lane.aspx
</SongUrl>
<ArtistUrl>
http://www.chartlyrics.com/cNVr9GhXLka4TMfJk7v0fg.aspx
</ArtistUrl>
<Artist>Eagles</Artist>
<Song>Life in the Fast Lane</Song>
<SongRank>9</SongRank>
</SearchLyricResult>
<SearchLyricResult>
<TrackId>0</TrackId>
<LyricChecksum>f40ac8bae81540469ead55d0cf57b7a1</LyricChecksum>
<LyricId>1588</LyricId>
<SongUrl>
http://www.chartlyrics.com/yxjb5R9LbUmjCFSLYRCQ0w/Anarchy+in+the+UK.aspx
</SongUrl>
<ArtistUrl>
http://www.chartlyrics.com/yxjb5R9LbUmjCFSLYRCQ0w.aspx
</ArtistUrl>
<Artist>Sex Pistols</Artist>
<Song>Anarchy in the UK</Song>
<SongRank>9</SongRank>
</SearchLyricResult>
<SearchLyricResult>
<TrackId>0</TrackId>
<LyricChecksum>e8a984143781f4bb95427b249e6edf3a</LyricChecksum>
<LyricId>2451</LyricId>
<SongUrl>
http://www.chartlyrics.com/lxqWV5Y390ufAqmFqJea6Q/Bomber.aspx
</SongUrl>
<ArtistUrl>
http://www.chartlyrics.com/lxqWV5Y390ufAqmFqJea6Q.aspx
</ArtistUrl>
<Artist>Motörhead</Artist>
<Song>Bomber</Song>
<SongRank>9</SongRank>
</SearchLyricResult>
<SearchLyricResult>
<TrackId>0</TrackId>
<LyricChecksum>95b7f4c731ea5a422e480eff154a8e6d</LyricChecksum>
<LyricId>3623</LyricId>
<SongUrl>
http://www.chartlyrics.com/_LsLsZ7P4EK-F-LD4dJgDQ/Helter+Skelter.aspx
</SongUrl>
<ArtistUrl>
http://www.chartlyrics.com/_LsLsZ7P4EK-F-LD4dJgDQ.aspx
</ArtistUrl>
<Artist>The Beatles</Artist>
<Song>Helter Skelter</Song>
<SongRank>9</SongRank>
</SearchLyricResult>
<SearchLyricResult>
<TrackId>0</TrackId>
<LyricChecksum>3a77790cede59201622ac25b301ce87d</LyricChecksum>
<LyricId>4293</LyricId>
<SongUrl>
http://www.chartlyrics.com/XdO8JqNWTU6K13vFeHw68Q/Let%27s+Talk+About+Sex.aspx
</SongUrl>
<ArtistUrl>
http://www.chartlyrics.com/XdO8JqNWTU6K13vFeHw68Q.aspx
</ArtistUrl>
<Artist>Salt-N-Pepa</Artist>
<Song>Let's Talk About Sex</Song>
<SongRank>9</SongRank>
</SearchLyricResult>
<SearchLyricResult>
<TrackId>0</TrackId>
<LyricChecksum>728c12ddcc6381fd404d2f6bc50dbce2</LyricChecksum>
<LyricId>3305</LyricId>
<SongUrl>
http://www.chartlyrics.com/xmpyap4BXEWLu1cad77VLg/Jailbreak.aspx
</SongUrl>
<ArtistUrl>
http://www.chartlyrics.com/xmpyap4BXEWLu1cad77VLg.aspx
</ArtistUrl>
<Artist>Thin Lizzy</Artist>
<Song>Jailbreak</Song>
<SongRank>9</SongRank>
</SearchLyricResult>
<SearchLyricResult>
<TrackId>0</TrackId>
<LyricChecksum>e651913c110a126e60cbd9673ff8fafe</LyricChecksum>
<LyricId>3068</LyricId>
<SongUrl>
http://www.chartlyrics.com/rV4f_rbSpkqgjcx2-HfBYA/It%27s+Like+That.aspx
</SongUrl>
<ArtistUrl>
http://www.chartlyrics.com/rV4f_rbSpkqgjcx2-HfBYA.aspx
</ArtistUrl>
<Artist>Run-D.M.C. vs. Jason Nevins</Artist>
<Song>It's Like That</Song>
<SongRank>9</SongRank>
</SearchLyricResult>
<SearchLyricResult>
<TrackId>0</TrackId>
<LyricChecksum>717d198977d72d5b0981082a3e332034</LyricChecksum>
<LyricId>8880</LyricId>
<SongUrl>
http://www.chartlyrics.com/NhBOlAOKEUaKoDFRWgXISA/All+Star.aspx
</SongUrl>
<ArtistUrl>
http://www.chartlyrics.com/NhBOlAOKEUaKoDFRWgXISA.aspx
</ArtistUrl>
<Artist>Smash Mouth</Artist>
<Song>All Star</Song>
<SongRank>9</SongRank>
</SearchLyricResult>
<SearchLyricResult>
<TrackId>0</TrackId>
<LyricChecksum>c8032ddab104a0bb786587d17a396010</LyricChecksum>
<LyricId>3819</LyricId>
<SongUrl>
http://www.chartlyrics.com/YJ5WLxsKuU-VpD3BUl0arQ/Quit+Playing+Games+(With+My+Heart).aspx
</SongUrl>
<ArtistUrl>
http://www.chartlyrics.com/YJ5WLxsKuU-VpD3BUl0arQ.aspx
</ArtistUrl>
<Artist>Backstreet Boys</Artist>
<Song>Quit Playing Games (With My Heart)</Song>
<SongRank>9</SongRank>
</SearchLyricResult>
<SearchLyricResult>
<TrackId>0</TrackId>
<LyricChecksum>0d4a8b407079cdb30f5a5a3f6ee8f974</LyricChecksum>
<LyricId>113</LyricId>
<SongUrl>
http://www.chartlyrics.com/osJmpBdl-0KhYBCHw7r9nw/Spit+It+Out.aspx
</SongUrl>
<ArtistUrl>
http://www.chartlyrics.com/osJmpBdl-0KhYBCHw7r9nw.aspx
</ArtistUrl>
<Artist>Slipknot</Artist>
<Song>Spit It Out</Song>
<SongRank>9</SongRank>
</SearchLyricResult>
<SearchLyricResult>
<TrackId>0</TrackId>
<LyricChecksum>34e4fdc09aab81fbd42170f3e83e7d50</LyricChecksum>
<LyricId>1307</LyricId>
<SongUrl>
http://www.chartlyrics.com/wqtScsjfpkqIny0WiyZUAw/Soul+Man.aspx
</SongUrl>
<ArtistUrl>
http://www.chartlyrics.com/wqtScsjfpkqIny0WiyZUAw.aspx
</ArtistUrl>
<Artist>Blues Brothers</Artist>
<Song>Soul Man</Song>
<SongRank>9</SongRank>
</SearchLyricResult>
<SearchLyricResult>
<TrackId>0</TrackId>
<LyricChecksum>b632cdcbd6ea0001c182ecbe24cff241</LyricChecksum>
<LyricId>2270</LyricId>
<SongUrl>
http://www.chartlyrics.com/n5ATvSkcJ0yodNSq8nxbGg/Sara.aspx
</SongUrl>
<ArtistUrl>
http://www.chartlyrics.com/n5ATvSkcJ0yodNSq8nxbGg.aspx
</ArtistUrl>
<Artist>Fleetwood Mac</Artist>
<Song>Sara</Song>
<SongRank>9</SongRank>
</SearchLyricResult>
<SearchLyricResult>
<TrackId>0</TrackId>
<LyricChecksum>202f72eb5b3d005bcb1d6543157c3224</LyricChecksum>
<LyricId>6742</LyricId>
<SongUrl>
http://www.chartlyrics.com/2cGCUdLHrU2voMz-rakhqA/The+Mob+Rules.aspx
</SongUrl>
<ArtistUrl>
http://www.chartlyrics.com/2cGCUdLHrU2voMz-rakhqA.aspx
</ArtistUrl>
<Artist>Black Sabbath</Artist>
<Song>The Mob Rules</Song>
<SongRank>9</SongRank>
</SearchLyricResult>
<SearchLyricResult>
<TrackId>0</TrackId>
<LyricChecksum>34abefd904136d0c38ac2eeaf16a3c8c</LyricChecksum>
<LyricId>1496</LyricId>
<SongUrl>
http://www.chartlyrics.com/a2v_0jD83EiJUgb52Pxk-A/School.aspx
</SongUrl>
<ArtistUrl>
http://www.chartlyrics.com/a2v_0jD83EiJUgb52Pxk-A.aspx
</ArtistUrl>
<Artist>Supertramp</Artist>
<Song>School</Song>
<SongRank>9</SongRank>
</SearchLyricResult>
<SearchLyricResult>
<TrackId>0</TrackId>
<LyricChecksum>930814c7dd18af406703a465c8529628</LyricChecksum>
<LyricId>1679</LyricId>
<SongUrl>
http://www.chartlyrics.com/iML6i8XMjUSVc8M-oqpcMA/Can%27t+Stop.aspx
</SongUrl>
<ArtistUrl>
http://www.chartlyrics.com/iML6i8XMjUSVc8M-oqpcMA.aspx
</ArtistUrl>
<Artist>Red Hot Chili Peppers</Artist>
<Song>Can't Stop</Song>
<SongRank>9</SongRank>
</SearchLyricResult>
<SearchLyricResult xsi:nil="true"/>
</ArrayOfSearchLyricResult>


My code is:

{
string apiurl = "http://api.chartlyrics.com/apiv1.asmx/SearchLyricText?lyricText=";
string lyric = System.Net.WebUtility.UrlEncode(input);
var myList = new List<string>();
XmlDocument xmlDoc = new XmlDocument();
XmlNamespaceManager ns = new XmlNamespaceManager(xmlDoc.NameTable);
ns.AddNamespace("ns", "http://api.chartlyrics.com/");
xmlDoc.Load(apiurl + lyric);
XmlNodeList songs = xmlDoc.SelectNodes("//SearchLyricResult", ns);
foreach (XmlNode song in songs)
{
XmlNode artistNode = song.SelectSingleNode("Artist");
XmlNode songNode = song.SelectSingleNode("Song");
if ((artistNode != null) && (songNode != null))
myList.Add(artistNode.InnerText + " - " + songNode.InnerText);
}
var results = myList.ToArray();
return results;
}


In my various Googling, I found some tips on XmlNamespaceManager which led me to add that in to the code, but this hasn't helped much. I believe the problem is in my XPath, because when I use
//*
nodes are selected and my foreach loop runs, but I don't get anything from the SelectSingleNode lines.
//*
didn't run before I added the namespace, so there was progress there. The XPaths I have tried are:

//ArrayOfSearchLyricResult/SearchLyricResult
// Returns nothing

//SearchLyricResult
// Returns nothing

//*
// Returns everything

Thanks in advance for any help.

Answer

You've done the right thing and created a namespace manager for your queries, but you have to actually use the namespace prefixes you've defined.

This should get you the results you're expecting:

XmlNodeList songs = xmlDoc.SelectNodes("/ns:ArrayOfSearchLyricResult/ns:SearchLyricResult", ns);
foreach (XmlNode song in songs)
{
    XmlNode artistNode = song.SelectSingleNode("ns:Artist");
    XmlNode songNode = song.SelectSingleNode("ns:Song");
    if ((artistNode != null) && (songNode != null))
        myList.Add(artistNode.InnerText + " - " + songNode.InnerText);
}