0
I am using a namespace at the moment I create my xml, but xmlns is also appearing in the tag below the root, this is hurting me, how can I make 'xmlns' get apernas in the root tag (Tlote_gnre)? Follows 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>
so, but the 'guides' tag is the daughter of the 'Tlote_gnre' tag, and the 'Tlote_gnre' tag needs the namespace I quoted.
– Felipe S
So, but then you just pass the namespace pro root element, not child element.
– Leonel Sanches da Silva
I gave an improved answer that I think was not clear enough. See now.
– Leonel Sanches da Silva
I understand what you mean now, but there is an error in the moment that xNamespace.Xmlns. " System.xml.Linq.Xnamespace value cannot be converted to 'System.xml.Linq.Xname'."
– Felipe S
Try to change
XNamespace.Xmlns
for"xmlns"
. Anything updates the answer.– Leonel Sanches da Silva
I used xmlns as string, but the xml output is identical to the one I mentioned, the 'tabs' tag still leaves with xmlns
– Felipe S
You’re right. I ate ball. "guides" also needs the namespace. Look now.
– Leonel Sanches da Silva
Friend, I think we’re talking about different things, the problem I’m trying is that in the tag daughter 'guides' is appearing the 'xmlns' and I want to leave it only at root, so I know that everything that comes after belongs to that namespace and so I can validate in Webservice, I say this because I’ve done some tests here the way I’m talking, but I did the xml on the nail
– Felipe S
So, but the child element needs to be the same namespace of the parent element (in its case, the root), otherwise the interpreter thinks that the child element belongs to another namespace. You tested now with this my latest edition?
– Leonel Sanches da Silva
I understood, the way you did the namespace really entered the tag daughter, but then I have to continue this xml, and I can’t find the tag daughter as <guides>, but without namespace I find, I do the following to find: 'xDoc.Elements. <guides>(0)'
– Felipe S
Well, then I guess that would be a question for another question.
– Leonel Sanches da Silva