How to display video from xml file?

左心房为你撑大大i 提交于 2019-12-10 21:30:57

问题


Hi am using xml file given below,how can i get videos from xml file?

<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>

My Code is

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();

but i got NullReferenceException error


回答1:


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")
             };

Or with your updated code:

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")
           };


来源:https://stackoverflow.com/questions/15335997/how-to-display-video-from-xml-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!