angel_neo
angel_neo

Reputation: 331

C# On Error resume

I have a problem: I'm reading multiple xml files from a directory, and I extract data from them. But If one or more xml files have errors or are bad formed the exception interrupts the process.

I know "on error resume next " is not a good practice, but How can I resume on error? to not interrupt...

try
    {
        foreach (string file in Directory.EnumerateFiles(path, "*.xml"))
        {
            xDoc.Load(System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), file));
            string strpath = xDoc.BaseURI;

            XmlNodeList nodeList = xDoc.SelectNodes("/Employees/Employee[*]"); //para que elija TODOS

            //Loop through the selected Nodes.
            foreach (XmlNode node in nodeList)
            {
                dr = CartDT.NewRow();
                dr["Employee_Id"] = node.Attributes["Id"].Value.ToString();
                dr["EmployeeName"] = node["EmployeeName"].InnerText;
                dr["City"] = node.Attributes["City"].Value.ToString();
                dr["Country"] = node["Country"].InnerText;
                dr["Comisiones"] = node["Comisiones"].InnerText;
                CartDT.Rows.Add(dr);


            }
        }
    }
    catch (System.Xml.XmlException)
    {

        drError = dtError.NewRow();
        //Here==> how can I continue the process?
    }
    gvXML.DataSource = CartDT;
    gvXML.DataBind();
    CartDT.Rows.Clear();
    }

Please, I hope anyone can help me. Thanks Best Regards...

Thanks Sami Kuhmonen You're right... that's the easiest way. And I can get the errors for make a little report:

//Crear las columnas del DataTable de Datos
    CartDT.Columns.Add("Employee_Id", typeof(string));
    CartDT.Columns.Add("EmployeeName", typeof(string));
    CartDT.Columns.Add("City", typeof(string));
    CartDT.Columns.Add("Country", typeof(string));
    CartDT.Columns.Add("Comisiones", typeof(string));

    //Crear las columnas del DataTable de Errores
    dtError.Columns.Add("Archivo", typeof(string));
    dtError.Columns.Add("Observaciones", typeof(string));


    foreach (string file in Directory.EnumerateFiles(path, "*.xml"))
    {
        try
        {
            xDoc.Load(System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), file));
            string strpath = xDoc.BaseURI;

            XmlNodeList nodeList = xDoc.SelectNodes("/Employees/Employee[*]"); //para que elija TODOS

            //Loop through the selected Nodes.
            foreach (XmlNode node in nodeList)
            {
                dr = CartDT.NewRow();
                dr["Employee_Id"] = node.Attributes["Id"].Value.ToString();
                dr["EmployeeName"] = node["EmployeeName"].InnerText;
                dr["City"] = node.Attributes["City"].Value.ToString();
                dr["Country"] = node["Country"].InnerText;
                dr["Comisiones"] = node["Comisiones"].InnerText;
                CartDT.Rows.Add(dr);
            }
        }
        catch (System.Xml.XmlException)
        {
            drError = dtError.NewRow();  //Preparamos fila para error
            drError["Archivo"] = Path.GetFileName(file); //Nombre del Archivo
            drError["Observaciones"] = "Error de Contenido XML";
            dtError.Rows.Add(drError);

            //mandar la informacion de error a la grilla
            gvError.DataSource = dtError;
            gvError.DataBind();
        }

    }

  //mandar la informacion a la grilla
    gvXML.DataSource = CartDT;
    gvXML.DataBind();

    CartDT.Rows.Clear(); //Limpiar el DataTable
    dtError.Rows.Clear();//Limpiar el DataTable de errores
}

Thanks a lot...

Upvotes: 1

Views: 1470

Answers (1)

Sami Kuhmonen
Sami Kuhmonen

Reputation: 31153

If you move the try...catch block inside the foreach loop. Then if one fails the others are still processed.

Upvotes: 5

Related Questions