我在使用XmlDocument.SelectNodes解析XML文檔時遇到問題。我試圖解析XML文檔下面是:XmlDocument.SelectNodes找不到筆記(C#)
<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>
我的代碼是:
{
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;
}
在我的各種谷歌搜索,我發現XmlNamespaceManager的一些技巧害得我在添加到代碼,但這並沒有太大的幫助。我相信這個問題出現在我的XPath中,因爲當我使用//*
節點時,我的foreach循環運行,但我沒有從SelectSingleNode行獲得任何東西。在添加命名空間之前,//*
沒有運行,所以有進展。我已經試過的XPath是:
//ArrayOfSearchLyricResult/SearchLyricResult
//返回什麼
//SearchLyricResult
//返回什麼
//*
//返回一切
預先感謝任何幫助。
就是這樣。謝謝!我知道這是我忽略的非常簡單的事情。有一件事,我必須改變(「ns:Artist」)和(「ns:Song」)到(「ns:Artist」,ns)和(「ns:Song」,ns)。否則就會拋出異常。 – NostraThomas
@NostraThomas好的。您需要在XPath使用名稱空間的任何時候傳遞'ns'。 – JLRishe