2013-12-17 30 views
2

我一直在四處搜尋,但我似乎無法找到有關此問題的任何信息。XDocument谷歌手機網站地圖標記

如果你去https://support.google.com/webmasters/answer/34648 它說,你必須將移動標籤添加到您的xml,它才能被正確抓取。 我的問題在於我不知道如何在使用XDocument時實際製作此標記。

有誰知道怎麼寫實際上可以此標記

<mobile:mobile/> 

使用的XElement標籤?

我有一個生成

XNamespace sitemap = XNamespace.Get("http://www.sitemaps.org/schemas/sitemap/0.9"); 
XNamespace mobile = XNamespace.Get("http://www.google.com/schemas/sitemap-mobile/1.0"); 

XDocument doc = new XDocument(
    new XDeclaration("1.0", "utf-8", null), 
    new XElement(sitemap + "urlset", 
     new XAttribute(XNamespace.Xmlns + "mobile", mobile)) 
); 

文檔和下面的代碼是構建一個元素

private XElement BuildSitemapItem(XNamespace ns) 
{ 
    XElement urlNode = new XElement(ns + "url", 
     new XElement(ns +"loc"), 
     new XElement(ns + "lastmod") 
    ); 

    return urlNode; 
} 

我一直停留在這個問題上了,而下面的代碼,因此任何幫助將是讚賞。

+0

您是否嘗試過簡單地增加一個'新的XElement(NS +「移動」 )'在'urlNode'構造函數內?雖然它與''不一樣,它意味着同樣的事情。 'mobile:'前綴只是一個簡短的命名空間參考。 –

+0

如果你添加它,它會將它添加到xmlns =「」我不知道它是否仍然是一樣的,我寧願它看起來完全一樣的壽,除非有人可以驗證谷歌不關心標籤看起來如何喜歡。 但是,謝謝。 –

+0

是的,它會添加'xmlns',但這不重要;它應該仍然是一樣的。 –

回答

1

你只需要指定你的XElement正確命名空間(在這種情況下,移動)

XNamespace mobileNs = "http://www.google.com/schemas/sitemap-mobile/1.0"; 
new XElement(mobileNs + "mobile") 

將輸出<mobile:mobile/>