我有一個XML文件,這樣的:獲取XML的innerText
<?xml version="1.0" encoding="utf-8"?>
<KWS>
<KW>
<KWNAME>"make money online youtube video"</KWNAME>
<GSC>318,000</GSC>
<YSC>821</YSC>
<TOP10>6</TOP10>
<TOP100>61</TOP100>
<SEARCH-US-C>0</SEARCH-US-C>
<NOTE>
</NOTE>
<YTLINKS>
<YT-LINK>
<LINK>http://www.youtube.com/watch?v=Dh5vptODX-M</LINK>
</YT-LINK>
<YT-LINK>
<LINK>http://www.youtube.com/watch?v=YtjvHX6VfcY</LINK>
</YT-LINK>
<YT-LINK>
<LINK>http://www.youtube.com/watch?v=WDfJoDCdvyw</LINK>
</YT-LINK>
<YT-LINK>
<LINK>http://www.youtube.com/watch?v=yRUEffXbokw</LINK>
</YT-LINK>
<YT-LINK>
<LINK>http://www.youtube.com/watch?v=YYsqgs5ve78</LINK>
</YT-LINK>
<YT-LINK>
<LINK>http://www.youtube.com/watch?v=7WaG3D-tSrs</LINK>
</YT-LINK>
</YTLINKS>
</KW>
<KW>
<KWNAME>"linkin park video youtube"</KWNAME>
<GSC>130,000</GSC>
<YSC>4,300</YSC>
<TOP10>7</TOP10>
<TOP100>69</TOP100>
<SEARCH-US-C>0</SEARCH-US-C>
<NOTE>
</NOTE>
<YTLINKS>
<YT-LINK>
<LINK>http://www.youtube.com/watch?v=ij34hCOMiIU</LINK>
</YT-LINK>
<YT-LINK>
<LINK>http://www.youtube.com/watch?v=mTMl5dRw8WI</LINK>
</YT-LINK>
<YT-LINK>
<LINK>http://www.youtube.com/watch?v=b-JehygMcJ8</LINK>
</YT-LINK>
<YT-LINK>
<LINK>http://www.youtube.com/watch?v=b2dmMxnUosc</LINK>
</YT-LINK>
<YT-LINK>
<LINK>http://www.youtube.com/watch?v=hZ62r3Q6ohA</LINK>
</YT-LINK>
<YT-LINK>
<LINK>http://www.youtube.com/watch?v=dREoOlTeYs4</LINK>
</YT-LINK>
<YT-LINK>
<LINK>http://www.youtube.com/watch?v=42xL8MG3xfQ</LINK>
</YT-LINK>
</YTLINKS>
</KW>
</KWS>
我的C#代碼來獲得YT-LINK
XmlDocument xml = new XmlDocument();
xml.Load(file);
XmlNodeList nodes = xml.SelectNodes("//KW");
foreach (XmlNode node in nodes)
{
KWDATA k = new KWDATA();
k.KEYWORD = node.SelectSingleNode("KWNAME").InnerText;
k.GSC = node.SelectSingleNode("GSC").InnerText;
k.YTC = node.SelectSingleNode("YSC").InnerText;
k.N_VIDEOS_TOP_10 = node.SelectSingleNode("TOP10").InnerText;
k.N_VIDEOS_TOP_100 = node.SelectSingleNode("TOP100").InnerText;
k.N_OF_KW_SEARCH_IN_GOOGLE = node.SelectSingleNode("SEARCH-US-C").InnerText;
List<string> YT_LINKS = new List<string>();
foreach (XmlNode node2 in node.SelectNodes("YTLINKS")) // <-- i know something is wrong here
{
MessageBox.Show(node2.SelectSingleNode("YT-LINK").InnerText);
YT_LINKS.Add(node2.SelectSingleNode("YT-LINK").InnerText);
}
k.YTLINKS = YT_LINKS;
內的每一個環節,但我只得到了第一個鏈接,就像每個條目:
http://www.youtube.com/watch?v=Dh5vptODX-M
http://www.youtube.com/watch?v=ij34hCOMiIU
只剩下其餘部分將不被檢索。
難道你不能使用LINQ to XML嗎?這在我看來更簡單 – BlackBear
是的,我可以但應用程序必須在.Net 2 – BOSS