3

嗨,我正在使用下面给出的 xml 文件,我如何从 xml 文件中获取视频?

<Category name="Videos">
   <article articleid="68">
     <videourl>
      <iframe src="http://player.vimeo.com/video/52375409?fullscreen=0" width="500"   height="298" frameborder="0"></iframe>
     </videourl>
    </article>
</Category>

我的代码是

XDocument loadedData = XDocument.Load("CountriesXML.xml");

        var data = from query in loadedData.Descendants("Country")
          select new CountryData
          {
             url = (string)query.Element("videourl").Elements("iframe").Single().Attribute("src").Value,
          };
     countryList = data.ToList();

但我得到 NullReferenceException 错误

4

1 回答 1

2
var xdoc = XDocument.Load("CountriesXML.xml");
var videos = from f in xdoc.Descendants("iframe")
             select new {
                Src = (string)f.Attribute("src"),
                Width = (int)f.Attribute("width"),
                Height = (int)f.Attribute("height")
             };

或者使用您更新的代码:

var xdoc = XDocument.Load("CountriesXML.xml");
var data = from c in xdoc.Descendants("Category") // you have Category element
           select new CountryData {
              url = (string)c.Element("article") // there is also article element
                             .Element("videourl")
                             .Elements("iframe")
                             .Single().Attribute("src")
           };
于 2013-03-11T10:18:49.527 回答