-2
I am structuring an XML file. And I need to Add another 'node' to my 'child node'.
The function that mounts XML:
private string MontarXML(List<Nota> listaNotas)
{
XmlDocument docConfig = new XmlDocument();
XmlNode xmlNode = docConfig.CreateNode(XmlNodeType.XmlDeclaration, "", "");
XmlElement rootElement = docConfig.CreateElement("NFSE");
docConfig.AppendChild(rootElement);
string cnpj = null;
for (var i = 0; i < listaNotas.Count; i++)
{
cnpj = listaNotas[0].Prestador.CpfCnpj;
var nota = listaNotas[i];
XmlElement notaIfo = docConfig.CreateElement("NfseInfo");
docConfig.DocumentElement.PrependChild(notaIfo);
docConfig.ChildNodes.Item(0).AppendChild(notaIfo);
XmlElement prestador = docConfig.CreateElement("Prestador");
docConfig.DocumentElement.PrependChild(prestador);
docConfig.ChildNodes.Item(0).AppendChild(prestador);
XmlElement outrasInformacoes = docConfig.CreateElement("OutrasInformacoes");
docConfig.DocumentElement.PrependChild(outrasInformacoes);
docConfig.ChildNodes.Item(0).AppendChild(outrasInformacoes);
XmlElement valores = docConfig.CreateElement("Valores");
docConfig.DocumentElement.PrependChild(valores);
docConfig.ChildNodes.Item(0).AppendChild(valores);
XmlElement environmentElement = docConfig.CreateElement("Numero");
XmlText environText = docConfig.CreateTextNode(nota.Numero.ToString());
environmentElement.AppendChild(environText);
notaIfo.PrependChild(environmentElement);
prestador.PrependChild(environmentElement);
environmentElement = docConfig.CreateElement("Competencia");
environText = docConfig.CreateTextNode(nota.Emissao.ToString("MM/yyyy"));
environmentElement.AppendChild(environText);
notaIfo.PrependChild(environmentElement);
prestador.PrependChild(environmentElement);
}
String path = "~/XML/"+cnpj;
bool pathExists = Directory.Exists(HttpContext.Current.Server.MapPath(path));
if (!pathExists)
{
Directory.CreateDirectory(HttpContext.Current.Server.MapPath(path));
}
Random rnd = new Random();
var x = rnd.Next(9999);
var data = DateTime.Now.ToString("dd-MM-yyyy");
var notasXML = path + "/" + cnpj + data + x + ".xml";
docConfig.Save(
HttpContext.Current.Server.MapPath(notasXML));
return notasXML;
}
Only the result is not as expected, by some coding failure of my function.
The output result:
<NFSE>
<NfseInfo>
</NfseInfo>
<Prestador>
<Competencia>04/2018</Competencia>
<Numero>1</Numero>
</Prestador>
<Valores />
I need . xml to structure this way.
<NFSe>
<NFSeInfo>
<NotaInfo>
<Numero>51</Numero>
<Emissao>23/07/2018</Emissao>
</NotaInfo>
<Valores>
<ValorCofins>15,63</ValorCofins>
</valores>
</NFSeInfo>
</NFSe>
I’d like to understand why the negatives.
– user108720
You are using the method
AppendChild()
wrong. You need to create an element and to raise the child need to do theAppendChild()
in the parent. For example: creates the nodeNFse
. Then create the nodeNFseInfo
To add a child knot to the parent do:NFSe.AppendChild(NFseInfo)
and so on– Guilherme Batista
I didn’t understand it well. I could illustrate. I need to add another knot, to the child knot.
– user108720