我有一个像这样的XML文件:
<BRPlugins>
<Plugin name="aaaa">
<summary>ssss</summary>
<description>dddd</description>
<category>cccc</category>
<product>pppp</product>
<gallery>
<media type="picture" url="http://linktofolder/image.jpg">
</media>
<media type="picture" url="http://linktofolder/image.jpg">
</media>
<media type="video" url="http://linktofolder/video.avi">
</media>
</gallery>
<Installers>
<Release version="1234" status="production">
<Installer url="http://linktofolder/installer1.exe"/>
<Installer url="http://linktofolder/installer2.exe"/>
<Installer url="http://linktofolder/installer3.exe"/>
</Release>
<Release version="4567" status="production">
<Installer url="http://linktofolder/installer4.exe"/>
<Installer url="http://linktofolder/installer5.exe"/>
<Installer url="http://linktofolder/installer6.exe"/>/>
</Release>
</Plugin>
</Plugins>
我正在使用XDocument读取XML并创建HTML:
XDocument xmlDoc = XDocument.Load(Server.MapPath("Plugins.xml"));
foreach (XElement product in xmlDoc.Root.Nodes())
{
counter++;
div += "<div class='col-plugin-item alt" + counter + "'><a href='javascript:void(0)' class='name' id='box" + counter + "'>" + product.Attribute("name").Value + "</a></div>";
}
testediv.InnerHtml = div;
我需要能够从媒体和安装程序访问元素“url”,以及从Release中访问版本和状态。我尝试了以下但是它不起作用:
var childElements = xmlDoc.Descendants("gallery").Elements("media");
foreach (XElement en in childElements)
{
div += "Descendents: " + en.Attributes("url");
}
testediv.InnerHtml = div;
感谢!!!
答案 0 :(得分:0)
当您致电Attributes("url")
时,即使只有一个元素,您实际上也在创建一个集合。这样的事情会更好:
div += "Descendents: " + en.Attributes("url").First().Value;