获取XML Innertext

时间:2012-11-16 15:34:36

标签: c# xml xml-parsing

我有一个像这样的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

仅限,其余部分将无法检索。

2 个答案:

答案 0 :(得分:1)

“YTLINKS”节点是单个节点,您可以先选择该节点,然后遍历以下内容中的“YTLINK”节点集合:

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

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

答案 1 :(得分: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();

            }
        }

}