xml文件的有效性验证

ajiang1234 2008-05-01 09:43:12
我把导入到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架构)
求解答
...全文
177 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
m2741454 2008-05-02
  • 打赏
  • 举报
回复
Xml.XmlValidatingReader 需要你的XMl架构信息 如果你只需要判断一个XMl是否合法 不需要用这个
直接加载 如果不合法 会加载不成功,不需要这种验证,这种验证和XmlSchemaValidator是一样的东西 ,需要一个架构信息,

就是说你的Xml必须满足一定的架构信息
主要是:某个元素下面必须出现什么元素,某个元素下面的可能出现的元素 属性的完整性等.
ajiang1234 2008-05-01
  • 打赏
  • 举报
回复
ding

16,552

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧