I am structuring an XML file. And I need to add another 'node' to my 'child node'.
The function that I mount 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 expected due to 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>