Adding a new node as a child is automatically adding the xmlns attribute
I'm trying to modify an XML document. The XML is structured like this:
<?xml version='1.0' encoding='iso-8859-1'?>
<Modelo39 xmlns="http://www.dgci.gov.pt/2002/OT" versao="1">
<Rosto>
<QuadroInicio />
<Quadro01>
<Q01C01>555555555</Q01C01>
</Quadro01>
<Quadro06>
<Rostoq06T>
</Rostoq06T>
</Quadro06>
</Rosto>
</Modelo39>
I'm trying to add to Rostoq06T a new node like this:
<Rostoq06T-Linha numero="1">
<NIF>100000000</NIF>
<CodRend>01</CodRend>
<Rendimento>2500</Rendimento>
<Retido>500</Retido>
</Rostoq06T-Linha>
I'm creating a new element with the name Rostoq06T-Linha and i'm adding it properly to the node Rosto06T:
XmlElement n开发者_JAVA技巧ode06T = xmlDoc.CreateElement("Rostoq06T-Linha");
node06T.SetAttribute("numero", linha.ToString());
//Here i add the elements to node06T
xmlDoc.DocumentElement.GetElementsByTagName("Rostoq06T").Item(0).AppendChild(node06T);
My problem is that the Rosto06T-Linha is being generated like this:
<Rostoq06T-Linha numero="1" xmlns="">
I can't figure out why it's being added the attribute xmlns to this node, if this element is a child of Rostoq06T.
Can anyone help me fixing this?
Because you created the Rostoq06T-Linha
element without namespace, ie. with empty namespace.
You must use the proper CreateElement
overload
XmlElement node06T = xmlDoc.CreateElement("Rostoq06T-Linha","http://www.dgci.gov.pt/2002/OT");
Unfortunately, you have to specify full namespace for all children you create!
精彩评论