XML Element Without Namespace - Sitemap

Last post 06-03-2008 10:00 AM by Cyberdash. 6 replies.

Sort Posts:

  • XML Element Without Namespace - Sitemap

    05-15-2008, 4:50 AM
    • Loading...
    • denniscy
    • Joined on 08-15-2007, 1:22 PM
    • Posts 135

    Hi, I am trying to create a google sitemap. But my url element which contains three other nodes always have an empty xmlns="" attribute.

    This is causing a namespace error when submitting the sitemap.

    Below is my code used to create the sitemap.

    I would be glad if someone can help me have a url element without the xmlns attribute

    I am trying to have this format:

    <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">

    <url>

       <loc></loc>

       <lastmod></lastmod>

       <changefreq></changefreq>

    </url>

    </urlset>

    Currently it is like below:

     <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">

    <url xmlns="">

       <loc></loc>

       <lastmod></lastmod>

       <changefreq></changefreq>

    </url>

    </urlset>

     

     -------------

    Try

    xmlDoc.Load("C:\sitemap.xml") Catch generatedExceptionName As System.IO.FileNotFoundException

    'if file is not found, create a new xml file

    Dim xmlWriter As New XmlTextWriter("C:\sitemap.xml", System.Text.Encoding.UTF8)

    xmlWriter.Formatting = Formatting.Indented

    xmlWriter.WriteStartDocument()

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

    xmlWriter.Close()

    End Try

    xmlDoc.Load("C:\sitemap.xml")

    Dim root As XmlNode = xmlDoc.DocumentElement

    Dim childNode As XmlNode = xmlDoc.CreateElement("url")Dim locNode As XmlNode = xmlDoc.CreateElement("loc")

    locNode.InnerText = url

    childNode.AppendChild(locNode)

    Dim lastModNode As XmlNode = xmlDoc.CreateElement("lastmod")

    lastModNode.InnerText = dateOfNews

    childNode.AppendChild(lastModNode)

    Dim changeNode As XmlNode = xmlDoc.CreateElement("changefreq")

    changeNode.InnerText = changeFreq

    childNode.AppendChild(changeNode)

    root.AppendChild(childNode)

    xmlDoc.Save("C:\sitemap.xml")

  • Re: XML Element Without Namespace - Sitemap

    05-15-2008, 4:57 AM

     

    I'm just guessing, but should there be a </urlset> at the end of your xml?  Maybe that's why the namespace is getting added to the next element.
    If this answered your question, be sure to mark it as the answer. That way, everybody after you will know it's the answer also!
  • Re: XML Element Without Namespace - Sitemap

    05-15-2008, 5:53 AM
    • Loading...
    • denniscy
    • Joined on 08-15-2007, 1:22 PM
    • Posts 135

    There is actually, sorry i forgot to put it for the example here.

  • Re: XML Element Without Namespace - Sitemap

    05-15-2008, 6:31 AM

    Another guess... Smile

    Does the order in which you append things matter?  (It doesn't seem like it should, but...)

    Maybe append the problem node before you add children to it, instead of waiting until the end?

     

     

    If this answered your question, be sure to mark it as the answer. That way, everybody after you will know it's the answer also!
  • Re: XML Element Without Namespace - Sitemap

    05-15-2008, 6:54 AM
    • Loading...
    • denniscy
    • Joined on 08-15-2007, 1:22 PM
    • Posts 135

    That didnt help but thanks for trying.

  • Re: XML Element Without Namespace - Sitemap

    05-15-2008, 8:53 AM
    Answer
    • Loading...
    • denniscy
    • Joined on 08-15-2007, 1:22 PM
    • Posts 135

    If anyone needs help with a similar problem , although not being the best practice(as it would be better not to have the xmlns attribute at all):

     1) use the innerXML property of the xml document to get the full xml data

     2)replace the unwanted characters using string.replace

     

     

     

  • Re: XML Element Without Namespace - Sitemap

    06-03-2008, 10:00 AM
    • Loading...
    • Cyberdash
    • Joined on 06-03-2008, 9:54 AM
    • Posts 1

    Hi! 

    I had the same problem, instead of this:

    denniscy:

    xmlWriter.WriteStartElement("urlset")

    xmlWriter.WriteAttributeString("xmlns", "", Nothing, "http://www.sitemaps.org/schemas/sitemap/0.9")

     

    Use:

    writer.WriteStartElement("urlset", "http://www.google.com/schemas/sitemap/0.84"); 

Page 1 of 1 (7 items)
Microsoft Communities
Page view counter