【发布时间】:2011-11-28 11:22:00
【问题描述】:
我有以下 Xml 结构:
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<sitemap>
<loc>http://www.example.com/</loc>
<lastmod>2011-11-27T08:34:46+00:00</lastmod>
</sitemap>
<sitemap>
<loc>http://www.example.com/123</loc>
<lastmod>2011-11-27T08:34:46+00:00</lastmod>
</sitemap>
</sitemapindex>
我想获取链接及其修改日期。例如结果应如下所示:
位置:http://www.example.com/ - lastmod:2011-11-27T08:34:46+00:00
位置:http://www.example.com/123 - lastmod:011-11-27T08:34:46+00:00
我使用了以下代码,但似乎没有任何效果:
XElement root = XElement.Load("data.xml");
var results = from el in root.Elements("sitemap")
select new
{
loc = (string) el.Element("loc"),
lastmod = (string) el.Element("lastmod")
};
foreach (var result in results)
{
Console.WriteLine("loc:" + result.loc + " - lastmod:" + result.lastmod);
}
即使这个查询也不返回任何内容:
var results = from el in root.Elements("sitemap")
select el;
我是 Linq to Xml 的新手,请帮忙。
【问题讨论】:
-
root.Elements().Where(element => element.Name.LocalName == "sitemap")可能也可以,但不建议这样做。
标签: linq linq-to-xml