Add a node to another child node in an XML

-1

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>
    
asked by anonymous 14.08.2018 / 14:37

1 answer

1

There are several ways to create an XML file. Here is an example of how you can create and iterate the nodes in a way that is similar to your idea.

        public void CreateDoc()
        {
            XmlNode parent, child;
            XmlDocument doc = new XmlDocument();
            parent = doc.CreateElement("NFSe");
            doc.AppendChild(parent);

            child = doc.CreateElement("NFSeInfo");
            parent.AppendChild(child);
            parent = child;

            child = doc.CreateElement("NotaInfo");
            parent.AppendChild(child);
            child = doc.CreateElement("Valores");
            parent.AppendChild(child);

            parent = parent.FirstChild;
            child = doc.CreateElement("Numero");
            child.InnerText = "51";
            parent.AppendChild(child);
            // e assim por diante...
        }
    
15.08.2018 / 23:16