0
I am developing a C# Console Application that loads a List and then writes an XML file with 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 wanted to generate XML more dynamically, without the need to clarify the fields, only with the contents of the list.
If there is any other simpler way I accept suggestions on how to proceed.
What do you mean
sem a necessidade de explicitar os campos
?– Jéf Bueno
In the
select
I have to put the names of the fieldsnew XAttribute("ID", emp.Id
and didn’t want to have this job.– Jothaz