【问题标题】:Load an xml file more than 0kb加载超过0kb的xml文件
【发布时间】:2015-06-07 05:48:10
【问题描述】:

我正在尝试解析 xml 文件,有很多文件,有些是 0kb。 当找到一个 0kb 的文件时,它会抛出一个异常 root element is missing 并结束。我尝试了一个 try catch 块,但在出错并且程序停止后它不会继续。

有人可以帮我解决这个问题吗?

【问题讨论】:

  • 只需加载文件并检查文件长度。如果大于零,则进行处理
  • 提问时显示您当前的代码

标签: c# xml


【解决方案1】:

您应该使用 FileInfo 类来检查文件大小,并且只有当长度大于零时才尝试解析 XML,如下所示:

var fileInfo = new System.IO.FileInfo(fileName); //where fileName is the full path to the XML file

if (fileInfo.Length > 0)
{
    //read the xml
}

【讨论】:

    猜你喜欢
    • 2018-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-09
    • 1970-01-01
    相关资源
    最近更新 更多