gpt4 book ai didi

C# 错误恢复

转载 作者:太空宇宙 更新时间:2023-11-03 19:53:26 25 4
gpt4 key购买 nike

我有一个问题:我正在从一个目录中读取多个 xml 文件,并从中提取数据。但是,如果一个或多个 xml 文件有错误或格式不正确,则异常会中断该过程。

我知道“on error resume next”不是一个好习惯,但是我怎样才能恢复错误?不要打扰...

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();
}

拜托,我希望有人能帮助我。谢谢最好的问候...

谢谢 Sami Kuhmonen你是对的......这是最简单的方法。我可以得到一些错误来做一个小报告:

//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
}

非常感谢...

最佳答案

如果将 try...catch block 移动到 foreach 循环中。然后,如果一个失败,其他的仍然被处理。

关于C# 错误恢复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36114911/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com