gpt4 book ai didi

linq - 如何显示来自xml文件的视频?

转载 作者:行者123 更新时间:2023-12-04 01:08:50 28 4
gpt4 key购买 nike

您好,我正在使用下面给出的 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 错误

最佳答案

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

关于linq - 如何显示来自xml文件的视频?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15335997/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com