Add a node to another child node in an XML

Asked

Viewed 215 times

-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.

  • 2

    You are using the method AppendChild() wrong. You need to create an element and to raise the child need to do the AppendChild() in the parent. For example: creates the node NFse. Then create the node NFseInfo To add a child knot to the parent do: NFSe.AppendChild(NFseInfo) and so on

  • I didn’t understand it well. I could illustrate. I need to add another knot, to the child knot.

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 similar way to your idea/way.

        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...
        }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.