2009-05-01 63 views
3

我想在ASP.NET MVC項目中創建一個站點地圖。如何將一點XML插入到使用Visual Basic的XDocument中?

這段代碼在我的節點控制器...

Function Sitemap() As ContentResult 
    Dim db As New EfrDotOrgEntities 
    Dim Nodes = db.Node.ToList 
    Dim RequestUrl As Uri = Url.RequestContext.HttpContext.Request.Url 
    Dim AbsoluteRoot As String = String.Format("{0}://{1}", RequestUrl.Scheme, RequestUrl.Authority) 

    Dim map As XDocument = <?xml version="1.0" encoding="UTF-8"?> 
          <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> 
          </urlset> 
    For Each n As Node In Nodes 
     map.Add(<url> 
        <loc><%= AbsoluteRoot + Url.RouteUrl("IdOnly", New With {.id = n.Id}) %></loc> 
       </url>) 
    Next 
    Return Content(map.ToString, "text/xml", Encoding.UTF8) 
End Function 

(這裏是一個形象,因爲堆棧溢出不着色VB代碼以及)

Visual Basic inline XML

...產生這個錯誤:

This operation would create an incorrectly structured document.

它不會知道wh添加該內容。

如何告訴它將該位的XML插入<urlset>

回答

5

你需要將它添加到頂層元素在文檔中(根):

map.Root.Add(...) 
+0

This works。謝謝! – 2009-05-01 12:19:22

+0

我一直在努力! – MattGWagner 2009-11-13 21:38:31

5

爲什麼不與其它XML文本孔完全填滿了呢?

Dim map As XDocument = <?xml version="1.0" encoding="UTF-8"?> 
         <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> 
          <%= From n In Nodes.Cast(Of Node)() _ 
           Select <url> 
              <loc><%= AbsoluteRoot + Url.RouteUrl("IdOnly", New With {.id = n.Id}) %></loc> 
             </url> %> 
         </urlset> 
相關問題