I'm developing a C # Console Application that loads a List and then writes an XML file to the contents of this List.
Class
public class Equipamento
{
public int Id { get; set; }
public string Marca { get; set; }
public int Ano { get; set; }
}
I'm writing XML like this:
private void GravaListXMLinq(List<Equipamento> ListaEquipamentos)
{
string caminho = @"D:\XML\saida.xml";
try
{
var xEle = new XElement("Equipamentos",
from emp in ListaEquipamentos
select new XElement("Equipamento",
new XAttribute("ID", emp.Id),
new XElement("Marca", emp.Marca),
new XElement("Ano", emp.Ano)
));
xEle.Save(caminho);
this.Log().Info("Arquivo XLM gravado em: " + caminho);
}
catch (Exception ex)
{
this.Log().Error("Ao gravar XML em: " + caminho + " Erro: " + ex.Message);
}
}
It works, but I wanted to generate the XML in a more dynamic way, without the need to explicit the fields, only with the contents of the list.
If there is a simpler way, I accept suggestions on how to proceed.