locked
How to insert XML into sqlite table? RRS feed

  • Question

  • User393015 posted

    Hello, I need to insert an xml in a table created in sqlite, this xml is 122 MB in size, but I don't know if I am doing it the right way, so far I have tried it this way, but it gives me an error. I also wish I could use a progress bar to know the insert process. Thank you!...

      async Task InsertaColores()
        {
            string rutaXml = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData), "C_XmlColores.xml");
            if (File.Exists(rutaXml))
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(rutaXml);
                DataSet dsc = new DataSet();
    
                using (XmlNodeReader xnr = new XmlNodeReader(xmlDoc))
                {
                    dsc.ReadXml(xnr);
                }
    
                DataTable dtc = dsc.Tables["XMLCOLORS"];                
    
                int cuentaFilasc = dtc.Rows.Count;
    
                _conn.CreateTable<tblColores>();               
    
                foreach (DataRow dr in dt.Rows)
                {
                    try
                    {
                        var color_id = dr["COLORS_ID"].ToString();
                        var color = dr["COLOR"].ToString();
    
                        tblColores tbl = new tblColores();
                        tbl.Color_Id = Convert.ToInt32(color_id);
                        tbl.Color = Convert.ToString(color);
                        _conn.Insert(tbl);
                    }
                    catch (SQLiteException ex)
                    {
                        await DisplayAlert("Error", ex.Message, "Ok");
                        btnInsertarArticulos.IsEnabled = true;
                    }
                }
    
    
                File.Delete(rutaXml);
    
                await DisplayAlert("Mensaje", "Se han insertado nuevos colores.", "Ok");
                //btnInsertarArticulos.IsEnabled = true;
            }
            else
            {
                await DisplayAlert("Mensaje", "El archivo xml con los colores no existe.", "Ok");
                btnInsertarArticulos.IsEnabled = true;
            }
        }
    
    Friday, May 1, 2020 1:19 AM

All replies

  • User393015 posted

    The error it gives me when trying to read the xml is the following: Unhandled Exception:

    System.Xml.XmlException:

    Friday, May 1, 2020 2:47 AM
  • User380187 posted

    Hi @delfinomorales , do you have a check this code with a small size of xml ? It seems the size of xml is too large , the foreach loop will do much things in the programa .

    Friday, May 1, 2020 7:11 AM
  • User393015 posted

    Hi @JuniorJiang, I have tested with an xml with size of 13 KB, and it works without problems, so it means that the xml i want to insert is very big, the xml cannot be loaded to the DataTable. Open some way to be able to work with a very large xml ?.

    Friday, May 1, 2020 10:04 PM