给定这样的站点地图:
<?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/