Empty namespace using Linq Xml

后端 未结 3 626
悲&欢浪女
悲&欢浪女 2020-12-14 15:11

I\'m trying to create a sitemap using Linq to Xml, but am getting an empty namespace attribute, which I would like to get rid of. e.g.

XNamespace ns = \"http         


        
3条回答
  •  北海茫月
    2020-12-14 15:54

    I stumbled across this post while dealing with a similar problem in VB.NET. I was using XML literals and it took me some searching to figure out how to make this solution work with the XML literal construction and not just the functional construction.

    The solution is to import the XML namespace at the top of the file.

    Imports 
    

    And then prefix all of my XML literals in the query expression with the imported namespace. This removes the empty namespace that were appearing on the elements when I saved my output.

    Dim output As XDocument = 
                                  
                                      
                                          0
                                          <%= From tg In termGroups _
                                              Select 
                                                         <%= From t In tg _
                                                             Select <%= t %> %>
                                                      %>
                                      
                                  
    
        output.Save("C:\thesaurus.xml")
    

    I hope this helps someone. Despite bumps in the road like this, the XLinq API is pretty darn cool.

提交回复
热议问题