Doubt with XML file creation

3

I have a program that creates an XML file using XmlTextWriter, following the example

public static void testeGerarXml()
{
    using (var xml = new XmlTextWriter(@"c:\Gustavo\teste.xml", Encoding.UTF8))
    {
        xml.WriteStartDocument();
        xml.Formatting = Formatting.None;

        xml.WriteStartElement("teste");
        {
            xml.WriteStartElement("endereco");
            {
                xml.WriteElementString("cep", "12345678");
                xml.WriteElementString("logradouro", "rua teste");
                xml.WriteElementString("numero", "112233");
            }
            xml.WriteEndElement();

            xml.WriteStartElement("contato");
            {
                xml.WriteElementString("celular", "(19) 9 9999-9999");
                xml.WriteElementString("email", "[email protected]");
                xml.WriteElementString("nome", "gustavo");
            }
            xml.WriteEndElement();
        }

        xml.WriteFullEndElement();
        xml.Close();
    }
}

I would like to create functions to facilitate the maintenance of the code, each function would be responsible for creating a node, as in the example.

public static void testeGerarXml()
{
    using (var xml = new XmlTextWriter(@"c:\Gustavo\teste.xml", Encoding.UTF8))
    {
        xml.WriteStartDocument();
        xml.Formatting = Formatting.None;

        xml.WriteStartElement("teste");
        {
            funcaoCriaEndereco();

            funcaoCriaContato();
        }

        xml.WriteFullEndElement();
        xml.Close();
    }
}


private static string funcaoCriaEndereco()
{
    using (var str = new StringWriter())
    {
        using (var xml = new XmlTextWriter(str))
        {
            xml.WriteStartDocument();
            xml.WriteStartElement("endereco");
            {
                xml.WriteElementString("cep", "12345678");
                xml.WriteElementString("logradouro", "rua teste");
                xml.WriteElementString("numero", "112233");
            }
            xml.WriteEndElement();

            return str.ToString();
        }
    }
}

Thank you.

    
asked by anonymous 04.04.2017 / 21:23

1 answer

4

I loved your idea, but the approach is not good. Use extensions for XmlWriter that will work best:

public static void testeGerarXml()
{
    using (var xml = new XmlTextWriter(@"c:\Gustavo\teste.xml", Encoding.UTF8))
    {
        xml.WriteStartDocument();
        xml.Formatting = Formatting.None;

        var endereco = new Endereco 
        {
            Cep = "12345",
            Logradouro = "Rua Tal",
            Numero = "345"
        };

        var contato = new Contato
        {
            Celular = "(11) 92222-2222",
            Email = "[email protected]",
            Nome = "Gustavo"
        };

        xml.EscreverEndereco(endereco);
        xml.EscreverContato(contato);

        xml.WriteFullEndElement();
        xml.Close();
    }
}

The methods:

public static class XmlWriterExtensions
{
    public static void EscreverEndereco(this XmlTextWriter xml, Endereco endereco)
    {
        // xml.WriteStartDocument();
        xml.WriteStartElement("endereco");
        {
            xml.WriteElementString("cep", endereco.Cep);
            xml.WriteElementString("logradouro", endereco.Logradouro);
            xml.WriteElementString("numero", endereco.Numero);
        }

        xml.WriteEndElement();
    }


    private static void EscreverContato(this XmlTextWriter xml, Contato contato)
    {
        // xml.WriteStartDocument();
        xml.WriteStartElement("contato");
        {
            xml.WriteElementString("celular", contato.Celular);
            xml.WriteElementString("email", contato.Email);
            xml.WriteElementString("nome", contato.Nome);
        }

        xml.WriteEndElement();
    }
}

Do not forget to declare Endereco and Contato .

    
04.04.2017 / 21:56