2012-11-16 83 views
0

我有一個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 

只剩下其餘部分將不被檢索。

+0

難道你不能使用LINQ to XML嗎?這在我看來更簡單 – BlackBear

+0

是的,我可以但應用程序必須在.Net 2 – BOSS

回答

1

的「YTLINKS」節點是一個單一的一個,你可以先選擇一個,然後通過的「YTLINK」節點內蒐集迭代:

var ytlinks = node.SelectSingleNode("YTLINKS"); 

foreach (XmlNode node2 in ytlinks.SelectNodes("YTLINK")) 
{   
    YT_LINKS.Add(node2.InnerText); 
} 
+0

感謝一百萬,它的工作像一個魅力謝謝。 – BOSS

0

這裏你去一些的LINQ to XML - 多少代碼

using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Text; 
    using System.Xml; 
    using System.Xml.Linq; 

    namespace xmltest 
    { 
     public class KwData 
     { 
      public string KEYWORD { get; set; } 
      public List<string> Links { get; set; } 
     } 

     class Program 
     { 
      static void Main(string[] args) 
      { 
       XDocument xml = XDocument.Load("XMLFile1.xml"); 

       List<KwData> KwDataList = new List<KwData>(); 

       // Use linq to XML 
       KwDataList = (from kwData in xml.Descendants("KW") 
        select new KwData 
        { 
         KEYWORD = kwData.Element("KWNAME").Value, 
         Links = kwData.Elements("YTLINKS").Elements("YT-LINK").Select(x => x.Value).ToList() 
        }).ToList(); 

      } 
     } 

}