XmlDocument.SelectNodes没有找到笔记(C#)

时间:2017-06-20 14:39:42

标签: c# xml xpath

我在使用XmlDocument.SelectNodes解析XML文档时遇到问题。我试图解析的XML文档如下:

<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 //不返回任何内容 //* //返回所有内容 提前感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

您已经做了正确的事情并为您的查询创建了命名空间管理器,但您必须实际使用您已定义的命名空间前缀。

这可以为您提供您期望的结果:

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

答案 1 :(得分:0)

也有类似的question。我想也可以帮忙,祝你有愉快的一天!

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

    //I'd change this part only
    XmlNodeList searchResult = xmlDoc.SelectNodes("/ns:ArrayOfSearchLyricResult/ns:SearchLyricResult", ns)
    foreach (XmlNode item in searchResult)
    {
        if (item != null)
        {
        myList.Add(item["Artist"].InnerText + " - " + example["Song"].InnerText;);
        }
    }

    var results = myList.ToArray();
    return results;
}