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.