2012-05-14 75 views
0

我想根據loc元素在我的google地圖上找到任何重複的內容。在google地圖上查找重複的地址

示例XML:

<?xml version="1.0" encoding="UTF-8"?> 
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xsi:s chemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <url><loc>http://mysite.net/Members.aspx</loc><lastmod>2011-07-01</lastmod></url>  
    <url><loc>http://mysite.net/Topics.aspx</loc><lastmod>2011-05-27</lastmod></url> 
    <url><loc>http://mysite.net/Members.aspx</loc><lastmod>2011-07-02</lastmod></url>  
</urlset> 

樣品LINQ:

  var duplicates = (from req in doc.Descendants("urlset") 
          group req by req.Descendants("//loc").First().Value 
           into g 
           where g.Count() > 1 
          select g.Skip(1)).SelectMany(elements => elements 
         ); 

怎麼會重複返回空?

回答

0

您的查詢沒有找到的元素,因爲你沒有指定命名空間。它也比必要的更復雜:

XNamespace ns = "http://www.sitemaps.org/schemas/sitemap/0.9"; 
var duplicates = 
    from loc in doc.Root.Elements(ns + "url").Elements(ns + "loc") 
    group loc by loc.Value into g 
    where g.Count() > 1 
    select g.Key; 
0

doc.Descendants("urlset")將永遠只返回一個元素(<urlset>)。

您需要選擇<url>元素:

from u in doc.Descendants("url") 
group u by u.Element("loc").Value into g 
from elem in g.Skip(1) //This is the SelectMany() 
select elem