如何使用 Visual Basic 将一些 XML 插入到 XDocument 中?
我正在尝试在 ASP.NET MVC 项目中创建站点地图。
我的 Node 控制器中的这段代码...
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
(这是一个图像,因为 Stack Overflow 无法很好地为 VB 代码着色)
...产生此错误:
此操作将创建一个 文档结构不正确。
它不知道在哪里添加该内容。
我如何告诉它将该部分 XML 插入到
中?
I'm trying to create a sitemap in an ASP.NET MVC project.
This code in my Node controller...
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
(here's an image because Stack Overflow doesn't color VB code well)
...produces this error:
This operation would create an
incorrectly structured document.
It wouldn't know where to add that content.
How do I tell it to insert that bit of XML into the <urlset>
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要将其添加到文档中的顶级元素(根):
You need to add it to the top level element in the document (the root):
为什么不使用另一个 xml 文字漏洞来完全填充它呢?
Why not fill it out completely with another xml literal hole?