有幾種方法來驗證XML ...
Public Shared Function IsValidXml(xmlString As String) As Boolean
Dim tagsWithData As New Regex("<\w+>[^<]+</\w+>")
If String.IsNullOrEmpty(xmlString) OrElse tagsWithData.IsMatch(xmlString) = False Then
Return False
End If
Try
Dim xmlDocument As New XmlDocument()
xmlDocument.LoadXml(xmlString)
Return True
Catch xmlException As XmlException
Return False
End Try
End Function
注:從here
採取或你可以簡單地處理該異常
Try
Dim document As XDocument = XDocument.Load("C:\Purchase Request Setup\Crystal reports\crptPurchaseRequest.xml")
Catch ex As XmlException 'Handle the exception
'Probably poorly formed XML...
End Try
驗證對什麼XML?您是否想確保XML是有效的XML,還是想要針對XSD驗證文件? –