Doubt with XML file creation

Asked

Viewed 54 times

3

I have a program that creates an XML file using Xmltextwriter, follow 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();
        }
    }
}

I even created a function that returns the generated node string, but I don’t know how to write this node in the main xml file, someone knows a way to make it work?

Thank you.

1 answer

4


I love your idea, but the approach is not good. Use Extensions to the XmlWriter that will work better:

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();
    }
}

Don’t forget to declare Endereco and Contato.

  • How does this Extensions that you quoted? Just create the class Xmlwriterextensions that the Xmltextwriter already recognizes the methods of the class as a method of it? I’m already going to test this code, but already thank peka help

  • 2

    "How does this Extensions you mentioned work? Just create the Xmlwriterextensions class that Xmltextwriter already recognizes the class methods as its method?" yes, you are extending it XmlWriter with two more new methods that are recognized even by Intellisense.

  • 2

    This idea with this answer was sensational.

  • 1

    Very good, it worked here. I did not know this Extensions, I read about it and it is very useful, Valew ai by the help. Hugs

Browser other questions tagged

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