0

我有一个这样的 xml 文件:

<?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 中的每个 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

只有,其余的将不会被检索。

4

2 回答 2

1

The "YTLINKS" node is a single one, you can select that one first, then iterate through the collection of "YTLINK" nodes within:

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

foreach (XmlNode node2 in ytlinks.SelectNodes("YTLINK"))
{           
    YT_LINKS.Add(node2.InnerText);
}
于 2012-11-16T15:39:14.440 回答
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();

            }
        }

}
于 2012-11-16T17:02:51.593 回答