Problems with Linq XML using .net namespace

0

I'm using a namespace when I create my xml, but xmlns is also appearing in the below-root tag, this is damaging to me, how can I make xmlns stay in the root tag (TLote_GNRE)? Here is the code:

Dim xDoc As New XDocument(New XDeclaration("1.0", "utf-8", Nothing))
Dim nS As XNamespace = "http://www.gnre.pe.gov.br"

Dim xGuias As XElement = New XElement("guias")
xDoc.Add(New XElement(nS + "TLote_GNRE", xGuias))

Dim sw As New StringWriter()
Dim xWrite As XmlWriter = XmlWriter.Create(sw)

xDoc.Save(xWrite)
xWrite.Close()

xDoc.Save("C:\xml.xml")

XML output:

<?xml version="1.0" encoding="UTF-8"?>
 <TLote_GNRE xmlns="http://www.gnre.pe.gov.br">
 <guias xmlns=""/>
</TLote_GNRE>
    
asked by anonymous 28.12.2015 / 13:00

1 answer

1

Removing the variable nS from here:

xDoc.Add(New XElement(nS + "TLote_GNRE", xGuias))

I think it confused the way you used it. The correct would be to use this:

Dim raiz As XElement = New XElement("TLote_GNRE", new XAttribute("Xmlns", ns))

Dim xGuias As XElement = New XElement(ns + "guias")
raiz.Add(xGuias)
xDoc.Add(raiz)

Note that "guias" also needs namespace .

    
28.12.2015 / 15:33