0
public T Exportar<T>(T obj, string nomeArquivo)
{
try
{
string file = Global.LocalPasta + nomeArquivo + ".xml";
if (File.Exists(file))
File.Delete(file);
using (var stream = new StreamWriter(file))
{
XmlSerializer serializador = new XmlSerializer(typeof(T));
serializador.Serialize(stream, obj);
}
return obj;
}
catch(Exception ex)
{
throw;
}
}
public T Importar<T>(string nomeArquivo, bool lista = false)
{
try
{
string file = Global.LocalPasta + nomeArquivo + ".xml";
XmlSerializer desserializador = new XmlSerializer(typeof(T));
T obj;
using (Stream reader = new FileStream(file, FileMode.Open))
{
obj = (T)desserializador.Deserialize(reader);
}
return obj;
}
catch (Exception ex)
{
throw;
}
}
there’s the xml
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfUsuario xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Usuario>
<dataHora>2019-06-03T17:42:00.776232-03:00</dataHora>
<excluido>false</excluido>
<idUsuarioGravou>1</idUsuarioGravou>
<idUsuario>0</idUsuario>
<cpf>11111111111</cpf>
<senha>123</senha>
</Usuario>
<Usuario>
<dataHora>2019-06-03T17:42:00.907262-03:00</dataHora>
<excluido>false</excluido>
<idUsuarioGravou>1</idUsuarioGravou>
<idUsuario>0</idUsuario>
<cpf>11111111111</cpf>
<senha>123</senha>
</Usuario>
<Usuario>
<dataHora>2019-06-03T17:42:00.907382-03:00</dataHora>
<excluido>false</excluido>
<idUsuarioGravou>1</idUsuarioGravou>
<idUsuario>0</idUsuario>
<cpf>11111111111</cpf>
<senha>123</senha>
</Usuario>
</ArrayOfUsuario>
Here is the method call
var X = new DTO().Importar<List<Usuario>>("Usuarios");
And where is the xml? The description of the error seems to be very clear
– Leandro Angelo
Gives an exception error with the message "root element is Missing" in the import function
– Jussi 7Virtual
responded with xml code
– Jussi 7Virtual
The import method is working, as long as the user class has its properties declared with the first lowercase letter as presented in XML. Did you check whether
Global.LocalPasta
is pointing to the right place?– Leandro Angelo
The problem has been solved?
– Pedro Paulo
the problem has been solved
– Jussi 7Virtual