【问题标题】:Error Loading Poorly Formatted XML File using VB.NET使用 VB.NET 加载格式不正确的 XML 文件时出错
【发布时间】:2021-04-13 15:39:39
【问题描述】:

我有一个 VB.Net 程序,它处理选定文件夹中的 XML 文件。问题是如果程序由于格式错误(即没有匹配的结束标记)而访问无效的 XML 文件,我会收到错误消息。在这些情况下,我只想跳过该文件并继续编写代码。这是发生错误并停止进程的地方:

XmlDocument.Load(XMLFile)

有谁知道我如何在加载之前测试文件是否是有效的 XML 文件,或者我可以在加载过程中放置​​一个错误句柄,以便在遇到此类文件时我的程序不会停止?谢谢!

【问题讨论】:

标签: xml vb.net


【解决方案1】:

您可以将负载包装在 Try-Catch 中。

    For Each XMLFile As String In SelectedFolder
        Try
            Dim xe As XElement = XElement.Load(XMLFile)
            'file is good
        Catch XMLex As Xml.XmlException
            'file is bad
            Stop 'comment/remove to ignore this error
        Catch ex As Exception
            'file is bad
            Stop 'comment/remove to ignore this error
        End Try
    Next

【讨论】:

    猜你喜欢
    • 2010-10-15
    • 2020-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多