"ROOT ELEMENT IS MISSING" ERROR WHEN TRYING TO DESIRIALIZE XML IN XAMARIN FORMS

Asked

Viewed 97 times

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

  • Gives an exception error with the message "root element is Missing" in the import function

  • responded with xml code

  • 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?

  • The problem has been solved?

  • the problem has been solved

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.