在同一个start元素标记内,前缀''无法从''重新定义为'http://www.sitemaps.org/schemas/sitemap/0.9'

时间:2010-09-27 16:26:31

标签: xml vb.net xml-namespaces xmlwriter

我收到此错误

  

前缀''无法在相同的起始元素标记内从''重新定义为'http://www.sitemaps.org/schemas/sitemap/0.9'

运行此代码时

Dim writer As XmlWriter = XmlWriter.Create(FileLocation + "StaticUrls3.xml")
Dim urlList As New List(Of String)

urlList.Add("link1")
urlList.Add("link2")
urlList.Add("link3")       

writer.WriteStartDocument()
writer.WriteStartElement("urlset")
writer.WriteAttributeString("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9")

For Each aUrl As String In urlList
    writer.WriteStartElement("url")
    writer.WriteElementString("loc", aUrl)
    writer.WriteEndElement()
Next

writer.WriteEndElement()
writer.WriteEndDocument()
writer.Close()

为什么我收到此错误?

1 个答案:

答案 0 :(得分:15)

试试这个:

Const siteMapNamespace As String = "http://www.sitemaps.org/schemas/sitemap/0.9"
Dim writer As XmlWriter = XmlWriter.Create(FileLocation + "StaticUrls3.xml")
Dim urlList As New List(Of String)

urlList.Add("link1")
urlList.Add("link2")
urlList.Add("link3")       

writer.WriteStartDocument()
writer.WriteStartElement("urlset", siteMapNamespace)

For Each aUrl As String In urlList
    writer.WriteStartElement("url", siteMapNamespace)
    writer.WriteElementString("loc", aUrl)
    writer.WriteEndElement()
Next

writer.WriteEndElement()
writer.WriteEndDocument()
writer.Close()