c# - 如何使用 XDocument linq 查询查询站点地图

标签 c# linq linq-to-xml sitemap

给定这样的站点地图:

<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
<sitemapindex xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\" xmlns:mobile=\"http://www.google.com/schemas/sitemap-mobile/1.0\">
<sitemap><loc>http://www.whyisxdocumentsuchapain.com/sitemap.kill.xml</loc><lastmod>2013-12-10T18:34:09Z</lastmod></sitemap>
<sitemap><loc>http://www.whyisxdocumentsuchapain.com/sitemap.destroy.xml</loc><lastmod>2013-12-10T18:34:09Z</lastmod></sitemap>
<sitemap><loc>http://www.whyisxdocumentsuchapain.com/sitemap.pillage.xml</loc><lastmod>2013-12-10T18:34:09Z</lastmod></sitemap>
<sitemap><loc>http://www.whyisxdocumentsuchapain.com/sitemap.etc.xml</loc><lastmod>2013-12-10T18:34:09Z</lastmod></sitemap>
</sitemapindex>

我如何使用 XDocument linq 查询获取 loc 节点内的四个 URL?

它似乎完全打败了我。

最佳答案

var ns = xdoc.Root.GetDefaultNamespace();
var urls = xdoc.Root.Elements().Elements(ns + "loc").Select(l => (string)l);

结果:

"http://www.whyisxdocumentsuchapain.com/sitemap.kill.xml",
"http://www.whyisxdocumentsuchapain.com/sitemap.destroy.xml"
"http://www.whyisxdocumentsuchapain.com/sitemap.pillage.xml"
"http://www.whyisxdocumentsuchapain.com/sitemap.etc.xml"

关于c# - 如何使用 XDocument linq 查询查询站点地图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20503299/

相关文章:

c# - 是否可以继承 XAML?

c# - 如何在控制台应用程序中获取光标的位置?

c# - 从列表中排除一个项目(按索引),并采取所有其他

c# - 嵌套列表上的 Linq C#

c# - XDocument.Load "Illegal characters in path."错误,但是当我测试函数时,有 XML

c# - 使用 linq 从 xml 获取特定节点

c# - HelloWorld 多线程 C# 应用程序

c# 区分大小写的 ASCII 排序?

c# - ElasticSearch NEST-先按GroupBy再按OrderBy

c# - 使用Linq解析出XML同级节点-C#