xml文件的有效性验证
我把导入到datagrid的xml文件进行一次有效性验证,代码如下:
Public Class Form1
Public ReadOnly Property schemafilename() As String
Get
Dim fileinfo As New IO.FileInfo(Application.ExecutablePath)
Dim foldername As String = fileinfo.DirectoryName
Return foldername & "\xbrlwendang.xml"
End Get
End Property
Private Sub btnload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnload.Click
dlgopenfile.Filter = "XML Files(*.xml)|*.xml|ALL Files(*.*)|*.*||"
If dlgopenfile.ShowDialog = Windows.Forms.DialogResult.OK Then
Try
Dim newdataset As New DataSet
newdataset.ReadXmlSchema(schemafilename)
Dim reader As New Xml.XmlTextReader(dlgopenfile.FileName)
Dim validatingreader As New Xml.XmlValidatingReader(reader)
validatingreader.Schemas.Add(Nothing, schemafilename)
Do While True
If validatingreader.Read = False Then
Exit Do
End If
Loop
reader.Close()
validatingreader.Close()
newdataset.ReadXml(dlgopenfile.FileName)
Me.DataGrid1.DataSource = newdataset
Catch ex As Exception
If ex.GetType Is GetType(Xml.Schema.XmlSchemaException) Then
MsgBox("the xml file is not valid:" & ex.Message)
Else
MsgBox("a general exception occured:" & ex.Message)
End If
End Try
End If
End Sub
End Class
可是不管我导入哪一个可以通过IE打开的xml文件,都出现了如下提示:
the xml file is not valid:应为架构根。请确保根元素是<schema>,并且命名空间为“http://www3.org/2001/XMLSchema”(对于XSD架构)或"urn:schemas-microsoft-com:xml-data"(对于XDR架构)
求解答