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.
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
– Gustavo Freitas
"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.– Leonel Sanches da Silva
This idea with this answer was sensational.
– Jéf Bueno
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
– Gustavo Freitas